如何在python中使用https服务使用web服务
问题描述:
我已经在web服务中使用了下面的python代码,但是在这里我的站点url是包含https的。所以如果我使用http,那么它会在Web服务运行时给出“未找到方法”错误,并且如果我使用https,那么此代码不起作用。如何在python中使用https服务使用web服务
任何人都可以让我知道如何使用https解决此问题。
import urllib
uid = username
pwd = psw
params = urllib.urlencode({
'uid': uid,
'pwd': pwd,
data = urllib.urlopen(url, params).read()
print data
或告诉我任何其他方法,我们可以使用Web服务。
在此先感谢!
答
影片有权利。尝试使用请求。功能示例:
def http_get(url, user=None, password=None, proxies=None, valid_response_status[httplib.OK], **kwargs):
"""
Performs a http get over an url
@param url: the url to perfom get against
@type url: str
@param user: user if authentication required
@type user: str
@param password: password if authentication required
@type password: str
@param proxies: proxies if required
@type proxies: dict
@param valid_response_status: http response status code considered as valid
@type valid_response_status: list of int
@raise exception: if the response status code is not in valid_response_status list
"""
auth = HTTPBasicAuth(username=user, password=password)
kwargs['proxies'] = proxies
kwargs['auth'] = auth
try:
LOGGER.debug("Performing http get over : %s" % url)
_request = requests.get(url, **kwargs)
if _request.status_code not in valid_response_status:
http_error_msg = '%s Error: %s' % (_request.status_code, _request.reason)
http_error = HTTPError(http_error_msg)
http_error.response = _request
raise http_error
return _request.content
except:
LOGGER.error("Error while performing http get over : %s" % url)
raise
您是否试过'urllib2'? http://docs.python.org/2/library/urllib2.html –
如果我使用urllib2然后urlencode将无法正常工作,它会给我错误,如果我简单地传递这些参数,那么它会给出错误。 –
那你为什么不使用'urllib'和'urllib2'中的'urlocode'呢?或者你可以看看http://docs.python-requests.org/en/latest/# – filmor