Python中的HTTPS请求 [英] HTTPS request in Python

查看:351
本文介绍了Python中的HTTPS请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python 3.2中通过HTTPS连接到网站。

I would like to connect to a site via HTTPS in Python 3.2.

我试过

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

但我得到了

    http.client.ResponseNotReady: Request-started

任何人都知道问题是什么?

Anyone know what the problem is?

推荐答案

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

First of all, if you just want to download something and don't want any special HTTP requests, you should use urllib.request instead of http.client.

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

如果你真的想使用http.client,你必须致电 发送请求标头后的endheaders

If you really want to use http.client, you must call endheaders after you send the request headers:

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

As putrequest / endheaders 的快捷方式,你也可以使用 request 方法,如这个:

As a shortcut to putrequest/endheaders, you can also use the request method, like this:

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

这篇关于Python中的HTTPS请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆