最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python3?http.client?網(wǎng)絡(luò)請(qǐng)求方式

 更新時(shí)間:2023年09月05日 11:10:45   作者:cocoajin  
這篇文章主要介紹了python3?http.client?網(wǎng)絡(luò)請(qǐng)求方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python調(diào)用攝像頭顯示圖像的實(shí)例

    python調(diào)用攝像頭顯示圖像的實(shí)例

    今天小編就為大家分享一篇python調(diào)用攝像頭顯示圖像的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • PyPI版本號(hào)重復(fù)發(fā)布問題解決

    PyPI版本號(hào)重復(fù)發(fā)布問題解決

    本文主要介紹了PyPI版本號(hào)重復(fù)發(fā)布問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-11-11
  • Python3 selenium 實(shí)現(xiàn)QQ群接龍自動(dòng)化功能

    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中

    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í)例

    今天小編就為大家分享一篇Pytorch卷積層手動(dòng)初始化權(quán)值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 在Python中使用Neo4j數(shù)據(jù)庫(kù)的教程

    在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)景

    淺談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種方法匯總

    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 Json讀寫操作之JsonPath用法詳解

    Python Json讀寫操作之JsonPath用法詳解

    JSONPath是一種信息抽取類庫(kù),是從JSON文檔中抽取指定信息的工具,提供多種語言實(shí)現(xiàn)版本,包括Javascript、Python、PHP和Java,這篇文章主要介紹了Python Json讀寫操作之JsonPath用法詳解,需要的朋友可以參考下
    2023-04-04
  • Python while循環(huán)的基本用法與終止條件

    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

最新評(píng)論

手游| 原阳县| 和田县| 庆云县| 大荔县| 和龙市| 延津县| 连城县| 天台县| 集贤县| 肇州县| 巩留县| 青铜峡市| 土默特左旗| 岢岚县| 清河县| 教育| 霍林郭勒市| 辉南县| 新乐市| 乐业县| 长丰县| 邳州市| 平利县| 合山市| 大同县| 大足县| 额尔古纳市| 秦安县| 宾阳县| 容城县| 垫江县| 林甸县| 会同县| 海淀区| 明水县| 罗平县| 馆陶县| 镇平县| 丹凤县| 历史|