python3?http.client?網(wǎng)絡請求方式
python3 http.client 網(wǎng)絡請求
一、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)關閉")#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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python3 selenium 實現(xiàn)QQ群接龍自動化功能
這篇文章主要介紹了Python3 selenium 實現(xiàn)QQ群接龍自動化功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Python實現(xiàn)將Word表格嵌入到Excel中
把Word中的表格轉(zhuǎn)到Excel中,順便做一個調(diào)整。這個需求在實際工作中,很多人還是經(jīng)常碰到的!本文就將介紹如何利用Python實現(xiàn)這一功能,需要的朋友可以了解一下2021-12-12
在Python中使用Neo4j數(shù)據(jù)庫的教程
這篇文章主要介紹了在Python中使用Neo4j數(shù)據(jù)庫的教程,Neo4j是一個具有一定人氣的非關系型的數(shù)據(jù)庫,需要的朋友可以參考下2015-04-04
淺談python中scipy.misc.logsumexp函數(shù)的運用場景
下面小編就為大家?guī)硪黄獪\談python中scipy.misc.logsumexp函數(shù)的運用場景。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06
Python while循環(huán)的基本用法與終止條件
本章詳細介紹了Python中的while循環(huán)的基本用法與終止條件,包括其重要性應用場景技術(shù)原理實現(xiàn)方法關鍵技術(shù)點等進階示例常見問題與解決方案最佳實踐等內(nèi)容最后總結(jié)了核心要點并提供思考題一步驟建議和延伸閱讀建議,需要的朋友可以參考下2026-05-05

