国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python發(fā)送HTTP請(qǐng)求的方法小結(jié)

2019-11-25 17:13:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 烟台市| 星子县| 盐亭县| 乌什县| 马尔康县| 桂阳县| 沛县| 阳西县| 内丘县| 广平县| 运城市| 彭阳县| 三穗县| 沅江市| 泾源县| 无为县| 赤峰市| 玉龙| 临高县| 邳州市| 修文县| 长阳| 邵武市| 易门县| 文水县| 青浦区| 台州市| 横山县| 清苑县| 南康市| 罗山县| 吉首市| 邻水| 凌云县| 加查县| 德令哈市| 中西区| 大连市| 酒泉市| 宝鸡市| 合江县|