0

我在下面使用了 web 服务的 python 代码,但这里我的网站 url 包括 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

或者告诉我我们可以使用 Web 服务的任何其他方法。

提前致谢!!!

4

1 回答 1

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
于 2013-08-30T13:06:29.723 回答