python發(fā)送HTTP請(qǐng)求的方法小結(jié)
本文實(shí)例講述了python發(fā)送HTTP請(qǐng)求的方法。分享給大家供大家參考。具體如下:
這里包含 Python 使用 GET/HEAD/POST 方法進(jìn)行 HTTP 請(qǐng)求
1. GET 方法:
>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> conn.request("GET", "/index.html")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> data1 = r1.read()
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print r2.status, r2.reason
404 Not Found
>>> data2 = r2.read()
>>> conn.close()
2. HEAD 方法:
>>> import httplib
>>> conn = httplib.HTTPConnection("www.python.org")
>>> conn.request("HEAD","/index.html")
>>> res = conn.getresponse()
>>> print res.status, res.reason
200 OK
>>> data = res.read()
>>> print len(data)
0
>>> data == ''
True
3. POST 方法:
>>> import httplib, urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
... "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("musi-cal.mojam.com:80")
>>> conn.request("POST", "/cgi-bin/query", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
200 OK
>>> data = response.read()
>>> conn.close()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python中OpenCV實(shí)現(xiàn)查找輪廓的實(shí)例
本文將結(jié)合實(shí)例代碼,介紹 OpenCV 如何查找輪廓、獲取邊界框。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Pytorch中torch.repeat_interleave()函數(shù)使用及說(shuō)明
這篇文章主要介紹了Pytorch中torch.repeat_interleave()函數(shù)使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
python使用numpy實(shí)現(xiàn)直方圖反向投影示例
今天小編就為大家分享一篇python使用numpy實(shí)現(xiàn)直方圖反向投影示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python實(shí)現(xiàn)兩個(gè)文件夾的同步
這篇文章主要為大家詳細(xì)介紹了利用python實(shí)現(xiàn)兩個(gè)文件夾的同步,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
python根據(jù)txt文本批量創(chuàng)建文件夾
這篇文章主要為大家詳細(xì)介紹了python根據(jù)txt文本批量創(chuàng)建文件夾,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
python實(shí)現(xiàn)連續(xù)變量最優(yōu)分箱詳解--CART算法
今天小編就為大家分享一篇python實(shí)現(xiàn)連續(xù)變量最優(yōu)分箱詳解--CART算法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

