Python http.client json 请求和响应.如何? [英] Python http.client json request and response. How?
问题描述
我有以下代码想要更新到 Python 3.x所需的库将更改为 http.client 和 json.
我似乎无法理解该怎么做.你能帮忙吗?
导入 urllib2导入jsondata = {"text": "Hello world github/linguist#1 **cool**, and #1!"}json_data = json.dumps(data)req = urllib2.Request("https://api.github.com/markdown")结果 = urllib2.urlopen(req, json_data)打印 '\n'.join(result.readlines())
import http.client导入jsonconnection = http.client.HTTPSConnection('api.github.com')标头 = {'内容类型':'应用程序/json'}foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}json_foo = json.dumps(foo)connection.request('POST', '/markdown', json_foo, headers)响应 = connection.getresponse()打印(响应.读取().解码())
我会带你完成它.首先,您需要创建一个 TCP 连接,用于与远程服务器进行通信.
<预><代码>>>>connection = http.client.HTTPSConnection('api.github.com')-- http.client.HTTPSConnection()
Thẹ̣n 您需要指定请求标头.
<预><代码>>>>标头 = {'内容类型':'应用程序/json'}在这种情况下,我们说请求正文的类型为 application/json.
接下来我们将从python dict()生成json数据
<预><代码>>>>foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}>>>json_foo = json.dumps(foo)然后我们通过 HTTPS 连接发送一个 HTTP 请求.
<预><代码>>>>connection.request('POST', '/markdown', json_foo, headers)获取回复并阅读.
<预><代码>>>>响应 = connection.getresponse()>>>response.read()b'<p>Hello world github/linguist#1 <strong>cool</strong>和#1!</p>'I have the following code that I'd like to update to Python 3.x The required libraries would change to http.client and json.
I can't seem to understand how to do it. Can you please help?
import urllib2
import json
data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
json_data = json.dumps(data)
req = urllib2.Request("https://api.github.com/markdown")
result = urllib2.urlopen(req, json_data)
print '\n'.join(result.readlines())
import http.client
import json
connection = http.client.HTTPSConnection('api.github.com')
headers = {'Content-type': 'application/json'}
foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}
json_foo = json.dumps(foo)
connection.request('POST', '/markdown', json_foo, headers)
response = connection.getresponse()
print(response.read().decode())
I will walk you through it. First you'll need to create a TCP connection that you will use to communicate with the remote server.
>>> connection = http.client.HTTPSConnection('api.github.com')
-- http.client.HTTPSConnection()
Thẹ̣n you will need to specify the request headers.
>>> headers = {'Content-type': 'application/json'}
In this case we're saying that the request body is of the type application/json.
Next we will generate the json data from a python dict()
>>> foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}
>>> json_foo = json.dumps(foo)
Then we send an HTTP request to over the HTTPS connection.
>>> connection.request('POST', '/markdown', json_foo, headers)
Get the response and read it.
>>> response = connection.getresponse()
>>> response.read()
b'<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'
这篇关于Python http.client json 请求和响应.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!