python - Python 中的 HTTPS 请求

标签 python https

我想在 Python 3.2 中通过 HTTPS 连接到一个站点。

我试过了

    conn = http.client.HTTPSConnection(urlStr, 8443)
    conn.putrequest('GET', '/')
    response = conn.getresponse()  
    print(response.read())

但是我明白了

    http.client.ResponseNotReady: Request-started

有人知道问题出在哪里吗?

最佳答案

首先,如果你只是想下载一些东西而不想要任何特殊的 HTTP 请求,你应该使用 urllib.request而不是 http.client

import urllib.request
r = urllib.request.urlopen('https://paypal.com/')
print(r.read())

如果你真的要使用http.client,你必须调用endheaders发送请求 header 后:

import http.client
conn = http.client.HTTPSConnection('paypal.com', 443)
conn.putrequest('GET', '/')
conn.endheaders() # <---
r = conn.getresponse()
print(r.read())

作为 putrequest/endheaders 的快捷方式,您还可以使用 request方法,像这样:

import http.client
conn = http.client.HTTPSConnection('paypal.com', 443)
conn.request('GET', '/') # <---
r = conn.getresponse()
print(r.read())

关于python - Python 中的 HTTPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9839179/

相关文章:

node.js - Socket.io 与 NGINX 和 http 2

r - 忽略R httr连接中的SSL错误

python - 在 emacs 中打开第二个 python 解释器

python - 使用 PyYAML 转储程序时如何添加类型标签?

python - 如何在python的服务器代码中指定xmlrpc错误代码

authentication - 客户端是否应该使用 GET 或 POST 获取 OAuth 2 访问 token ?

java - 在X509TrustManager中,方法 "getAcceptedIssuers"有什么用?

带有用户名和密码的 c# VssBasicCredential

python - 如何在 jupyter notebook 中将 tqdm 与 pandas 一起使用?

python - RESTful-Flask 使用 parse_args() 解析 JSON 数组