如何在python中使用https的Web服务

时间:2013-08-30 12:05:43

标签: python web-services

我在下面使用python代码进行网络服务,但这里我的网站网址是包含https。因此,如果我使用http然后它在Web服务运行时给出“找不到方法”错误,如果我使用https,那么此代码不起作用。

任何人都让我知道如何使用https解决此问题。

import urllib
uid = username
pwd = psw   
params = urllib.urlencode({
    'uid': uid,
    'pwd': pwd,
data = urllib.urlopen(url, params).read()
print data

或告诉我任何其他可以使用网络服务的方法。

提前致谢!!!

1 个答案:

答案 0 :(得分:0)

电影有权利。尝试使用请求。功能的一个例子:

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