栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python http.client json请求和响应。怎么样?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python http.client json请求和响应。怎么样?

import http.clientimport jsonconnection = 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().depre())

我会引导您完成。首先,您需要创建一个TCP连接,用于与远程服务器进行通信。

>>> connection = http.client.HTTPSConnection('api.github.com')

-

http.client.HTTPSConnection()

然后,您将需要指定请求标头。

>>> headers = {'Content-type': 'application/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)

获取响应并阅读。

>>> response = connection.getresponse()>>> response.read()b'<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号