python3?http.client?網(wǎng)絡(luò)請求方式
python3 http.client 網(wǎng)絡(luò)請求
一、get 請求
'''
Created on 2014年4月21日
@author: dev.keke@gmail.com
'''
import http.client
#簡單的GET請求
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())
#測試一個無效的請求
inCon = http.client.HTTPConnection('www.baidu.com')
inCon.request('GET', 'None.html')
resu2 = inCon.getresponse()
print('\n')
print(resu2.status,resu2.msg)二、POST 請求
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 請求
>>> 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 請求
>>> # 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使用實例
使用實例
# -*- 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python中import與from方法總結(jié)(推薦)
這篇文章主要介紹了python中import與from方法總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
一文掌握python中的__init__的意思及使用場景分析
__init__是構(gòu)造方法,誰調(diào)用,表示誰(更直觀的理解就是類的方法中,誰調(diào)用,表示誰,見下面第一個代碼)!!并不是必選項,也就是說在類中,這個不是必須用的,那什么場景需要用到,什么場景不需要用到呢,感興趣的朋友跟隨小編一起看看吧2023-02-02
Python人工智能之路 之PyAudio 實現(xiàn)錄音 自動化交互實現(xiàn)問答
關(guān)于音頻, PyAudio 這個庫, 可以實現(xiàn)開啟麥克風(fēng)錄音, 可以播放音頻文件等等。文章介紹了如何使用Python第三方庫PyAudio進行麥克風(fēng)錄音然后自動播放已經(jīng)合成的語音實現(xiàn)語音交互回答,需要的朋友可以參考下2019-08-08
Python?內(nèi)置模塊?argparse快速入門教程
argparse模塊是Python內(nèi)置的用于命令項選項與參數(shù)解析的模塊,argparse模塊可以讓人輕松編寫用戶友好的命令行接口,能夠幫助程序員為模型定義參數(shù),這篇文章主要介紹了快速入門Python內(nèi)置模塊argparse,需要的朋友可以參考下2023-06-06
Python實現(xiàn)數(shù)據(jù)庫并行讀取和寫入實例
本篇文章主要介紹了Python實現(xiàn)數(shù)據(jù)庫并行讀取和寫入實例,非常具有實用價值,需要的朋友可以參考下2017-06-06
TensorFlow實現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件
今天小編就為大家分享一篇TensorFlow實現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

