python發(fā)送HTTP請求的方法小結(jié)
更新時間:2015年07月08日 11:29:18 作者:鑒客
這篇文章主要介紹了python發(fā)送HTTP請求的方法,實例總結(jié)了GET、HEAD與POST方式發(fā)送http請求的相關(guān)技巧,需要的朋友可以參考下
本文實例講述了python發(fā)送HTTP請求的方法。分享給大家供大家參考。具體如下:
這里包含 Python 使用 GET/HEAD/POST 方法進(jìn)行 HTTP 請求
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()
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Pytorch中torch.repeat_interleave()函數(shù)使用及說明
這篇文章主要介紹了Pytorch中torch.repeat_interleave()函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
python使用numpy實現(xiàn)直方圖反向投影示例
今天小編就為大家分享一篇python使用numpy實現(xiàn)直方圖反向投影示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python根據(jù)txt文本批量創(chuàng)建文件夾
這篇文章主要為大家詳細(xì)介紹了python根據(jù)txt文本批量創(chuàng)建文件夾,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
python實現(xiàn)連續(xù)變量最優(yōu)分箱詳解--CART算法
今天小編就為大家分享一篇python實現(xiàn)連續(xù)變量最優(yōu)分箱詳解--CART算法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實現(xiàn)
今天小編就為大家分享一篇關(guān)于ResNeXt網(wǎng)絡(luò)的pytorch實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

