相当于请求中的curl代码

时间:2014-01-14 14:13:22

标签: curl python-requests

我有以下curl,我需要使用request包从python中工作。

curl -s -H 'Accept: application/xml' 'https://xyz.com'

我尝试了以下但没有成功。你能看出我在这里做错了什么吗?

headers = {'Accept': 'application/xml'} 
print requests.get("https://xyz.com", headers=headers)

几秒钟后我得到了这个:

print requests.get("https://xyz.com", headers=headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 389, in send
raise SSLError(e)
requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

修复:调用不等同,因为curl调用是http,而请求调用是https。

1 个答案:

答案 0 :(得分:0)

您可以添加verify参数进行SSL验证,如下所示:

requests.get("https://xyz.com", headers=headers, verify=True)

希望这会有所帮助;)