国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python http.client json請求和響應。怎么樣?

瀏覽:4日期:2022-08-07 14:58:18
如何解決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().decode())

我會引導您完成。首先,您需要創建一個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>’解決方法

我有以下代碼想要更新為Python 3.x,所需的庫將更改為http.client和json。

我似乎不明白該怎么做。你能幫忙嗎?

import urllib2import jsondata = {'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())

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 永川市| 罗城| 若尔盖县| 衡山县| 马公市| 崇阳县| 铅山县| 娱乐| 兰考县| 牟定县| 德格县| 东阿县| 沾化县| 玛纳斯县| 宜君县| 曲松县| 延津县| 白玉县| 石嘴山市| 文登市| 福泉市| 望奎县| 古田县| 太保市| 伽师县| 中宁县| 福海县| 谢通门县| 晋江市| 米脂县| 普宁市| 杭锦后旗| 阳高县| 祥云县| 重庆市| 茶陵县| 高雄县| 建德市| 灵石县| 嘉禾县| 武强县|