python3?http.client?網(wǎng)絡(luò)請(qǐng)求方式
python3 http.client 網(wǎng)絡(luò)請(qǐng)求
一、get 請(qǐng)求
'''
Created on 2014年4月21日
@author: dev.keke@gmail.com
'''
import http.client
#簡(jiǎn)單的GET請(qǐng)求
con = http.client.HTTPConnection('www.baidu.com')
con.request("GET", "/index.html",'',{})
resu = con.getresponse()
print(resu.status,resu.reason,resu.info()) #打印讀取到的數(shù)據(jù)
#打印讀取的數(shù)據(jù)
print (resu.read())
#測(cè)試一個(gè)無效的請(qǐng)求
inCon = http.client.HTTPConnection('www.baidu.com')
inCon.request('GET', 'None.html')
resu2 = inCon.getresponse()
print('\n')
print(resu2.status,resu2.msg)二、POST 請(qǐng)求
import http.client,urllib.parse
pararms = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = http.client.HTTPConnection("bugs.python.org")
conn.request('POST', '', pararms, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(data)
conn.close()打印結(jié)果 :
302 Found
b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
三、head 請(qǐng)求
>>> import http.client
>>> conn = http.client.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 == b''
True四、put 請(qǐng)求
>>> # This creates an HTTP message
>>> # with the content of BODY as the enclosed representation
>>> # for the resource http://localhost:8080/file
...
>>> import http.client
>>> BODY = "***filecontents***"
>>> conn = http.client.HTTPConnection("localhost", 8080)
>>> conn.request("PUT", "/file", BODY)
>>> response = conn.getresponse()
>>> print(response.status, response.reason)
200, OK參考:
https://docs.python.org/3.4/library/http.client.html?highlight=http.client#module-http.client
python3 http.client使用實(shí)例
使用實(shí)例
# -*- coding: utf-8 -*-
# @Time : 2020/6/8 5:24 下午
# @Author : renwoxing
# @File : httpclient.py
# @Software: PyCharm
import http.client
if __name__ == '__main__':
headers = {
"Connection": "keep-alive",
}
conn = http.client.HTTPConnection('10.9.1.17:8000')
for var in range(100):
conn.request('GET', '/json_test', None, headers)
res = conn.getresponse()
print(res.status, res.code)
conn.close()
print("連接已經(jīng)關(guān)閉")#coding=utf-8
import http.client, urllib.parse
import http.client, urllib.parse
import random
USER_AGENTS = [
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
]
def get_demo(num,keyword):
page = urllib.parse.urlencode({'page':num})
params = urllib.parse.urlencode({})
headers = {'Referer': 'http://t66y.com/index.php',
'User-Agent': random.choice(USER_AGENTS ),
'Accept-Language': 'zh-CN,zh;q=0.9',
}
conn = http.client.HTTPConnection("t66y.com", timeout=10)
conn.request("GET", "/thread0806.php?fid=22&"+page, params, headers)
r1 = conn.getresponse()
#print(r1.read())
html = r1.read()
data = html.decode('gbk') # This will return entire content.
content = data.find(keyword)
if content != -1:
print('bingo:'+page)
else:
print('try {},status:{}'.format(page, r1.status))
def post_demo():
params = urllib.parse.urlencode({'qruuid': 'asdf', 'user_uuid': '3423412dfasf'})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "application/json"}
conn = http.client.HTTPSConnection("wx.coderr.cn")
conn.request("POST", "/api/qrcode", params, headers)
response = conn.getresponse()
print(response.status, response.reason)
if not response.closed:
data = response.read()
print(data, type(data.decode('utf-8')))
conn.close()
if __name__ == '__main__':
pass參考范例:
https://www.journaldev.com/19213/python-http-client-request-get-post
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python?Httpx庫(kù)實(shí)現(xiàn)超跑式網(wǎng)絡(luò)請(qǐng)求用法實(shí)例
- Python發(fā)送網(wǎng)絡(luò)請(qǐng)求(requests)
- Python網(wǎng)絡(luò)請(qǐng)求之Requests庫(kù)的高級(jí)功能運(yùn)用
- Python網(wǎng)絡(luò)請(qǐng)求使用Requests庫(kù)抓取解析數(shù)據(jù)
- Python?async+request與async+aiohttp實(shí)現(xiàn)異步網(wǎng)絡(luò)請(qǐng)求探索
- Python中網(wǎng)絡(luò)請(qǐng)求中Retry策略實(shí)現(xiàn)方式
相關(guān)文章
Python3 selenium 實(shí)現(xiàn)QQ群接龍自動(dòng)化功能
這篇文章主要介紹了Python3 selenium 實(shí)現(xiàn)QQ群接龍自動(dòng)化功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python實(shí)現(xiàn)將Word表格嵌入到Excel中
把Word中的表格轉(zhuǎn)到Excel中,順便做一個(gè)調(diào)整。這個(gè)需求在實(shí)際工作中,很多人還是經(jīng)常碰到的!本文就將介紹如何利用Python實(shí)現(xiàn)這一功能,需要的朋友可以了解一下2021-12-12
Pytorch卷積層手動(dòng)初始化權(quán)值的實(shí)例
今天小編就為大家分享一篇Pytorch卷積層手動(dòng)初始化權(quán)值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
在Python中使用Neo4j數(shù)據(jù)庫(kù)的教程
這篇文章主要介紹了在Python中使用Neo4j數(shù)據(jù)庫(kù)的教程,Neo4j是一個(gè)具有一定人氣的非關(guān)系型的數(shù)據(jù)庫(kù),需要的朋友可以參考下2015-04-04
淺談python中scipy.misc.logsumexp函數(shù)的運(yùn)用場(chǎng)景
下面小編就為大家?guī)硪黄獪\談python中scipy.misc.logsumexp函數(shù)的運(yùn)用場(chǎng)景。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
Pytorch隨機(jī)數(shù)生成常用的4種方法匯總
隨機(jī)數(shù)廣泛應(yīng)用在科學(xué)研究,但是計(jì)算機(jī)無法產(chǎn)生真正的隨機(jī)數(shù),一般成為偽隨機(jī)數(shù),下面這篇文章主要給大家介紹了關(guān)于Pytorch隨機(jī)數(shù)生成常用的4種方法,需要的朋友可以參考下2023-05-05
Python while循環(huán)的基本用法與終止條件
本章詳細(xì)介紹了Python中的while循環(huán)的基本用法與終止條件,包括其重要性應(yīng)用場(chǎng)景技術(shù)原理實(shí)現(xiàn)方法關(guān)鍵技術(shù)點(diǎn)等進(jìn)階示例常見問題與解決方案最佳實(shí)踐等內(nèi)容最后總結(jié)了核心要點(diǎn)并提供思考題一步驟建議和延伸閱讀建議,需要的朋友可以參考下2026-05-05

