欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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())
#測試一個(gè)無效的請求
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使用實(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中import與from方法總結(jié)(推薦)

    python中import與from方法總結(jié)(推薦)

    這篇文章主要介紹了python中import與from方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 一文掌握python中的__init__的意思及使用場景分析

    一文掌握python中的__init__的意思及使用場景分析

    __init__是構(gòu)造方法,誰調(diào)用,表示誰(更直觀的理解就是類的方法中,誰調(diào)用,表示誰,見下面第一個(gè)代碼)?。〔⒉皇潜剡x項(xiàng),也就是說在類中,這個(gè)不是必須用的,那什么場景需要用到,什么場景不需要用到呢,感興趣的朋友跟隨小編一起看看吧
    2023-02-02
  • python流程圖和思維導(dǎo)圖實(shí)例代碼

    python流程圖和思維導(dǎo)圖實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于python流程圖和思維導(dǎo)圖的相關(guān)資料,學(xué)習(xí)python過程中,畫流程圖可以有效的幫助你梳理程序的邏輯,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-08-08
  • Python人工智能之路 之PyAudio 實(shí)現(xiàn)錄音 自動化交互實(shí)現(xiàn)問答

    Python人工智能之路 之PyAudio 實(shí)現(xiàn)錄音 自動化交互實(shí)現(xiàn)問答

    關(guān)于音頻, PyAudio 這個(gè)庫, 可以實(shí)現(xiàn)開啟麥克風(fēng)錄音, 可以播放音頻文件等等。文章介紹了如何使用Python第三方庫PyAudio進(jìn)行麥克風(fēng)錄音然后自動播放已經(jīng)合成的語音實(shí)現(xiàn)語音交互回答,需要的朋友可以參考下
    2019-08-08
  • 基于Python編寫一個(gè)PDF轉(zhuǎn)換工具箱

    基于Python編寫一個(gè)PDF轉(zhuǎn)換工具箱

    這篇文章主要為大家詳細(xì)介紹了如何使用Python編寫一個(gè)PDF轉(zhuǎn)換工具箱,可以實(shí)現(xiàn)PDF轉(zhuǎn)圖片,word,拆分,刪除,提取等功能,感興趣的可以了解下
    2024-12-12
  • Python中列表的常用操作詳解

    Python中列表的常用操作詳解

    這篇文章主要為大家詳細(xì)介紹了python字典的常用操作方法,主要內(nèi)容包含Python中列表(List)的詳解操作方法,包含創(chuàng)建、訪問、更新、刪除、其它操作等,需要的朋友可以參考下
    2021-09-09
  • Python?內(nèi)置模塊?argparse快速入門教程

    Python?內(nèi)置模塊?argparse快速入門教程

    argparse模塊是Python內(nèi)置的用于命令項(xiàng)選項(xiàng)與參數(shù)解析的模塊,argparse模塊可以讓人輕松編寫用戶友好的命令行接口,能夠幫助程序員為模型定義參數(shù),這篇文章主要介紹了快速入門Python內(nèi)置模塊argparse,需要的朋友可以參考下
    2023-06-06
  • Python實(shí)現(xiàn)數(shù)據(jù)庫并行讀取和寫入實(shí)例

    Python實(shí)現(xiàn)數(shù)據(jù)庫并行讀取和寫入實(shí)例

    本篇文章主要介紹了Python實(shí)現(xiàn)數(shù)據(jù)庫并行讀取和寫入實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-06-06
  • TensorFlow實(shí)現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件

    TensorFlow實(shí)現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件

    今天小編就為大家分享一篇TensorFlow實(shí)現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Django模板過濾器和繼承示例詳解

    Django模板過濾器和繼承示例詳解

    初入python和django做項(xiàng)目,遇到很多前端頁面代碼冗余的情況,特別是頭部和腳部,代碼都是一樣的,所以下面這篇文章主要給大家介紹了關(guān)于Django模板過濾器和繼承的相關(guān)資料,需要的朋友可以參考下
    2021-11-11

最新評論