欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

对python3标准库httpclient的使用详解

程序员文章站 2022-05-31 09:10:02
如下所示: import http.client, urllib.parse import http.client, urllib.parse import...

如下所示:

import http.client, urllib.parse
import http.client, urllib.parse
import random

user_agents = [
 "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1; acoobrowser; .net clr 1.1.4322; .net clr 2.0.50727)",
 "mozilla/4.0 (compatible; msie 7.0; windows nt 6.0; acoo browser; slcc1; .net clr 2.0.50727; media center pc 5.0; .net clr 3.0.04506)",
 "mozilla/4.0 (compatible; msie 7.0; aol 9.5; aolbuild 4337.35; windows nt 5.1; .net clr 1.1.4322; .net clr 2.0.50727)",
 "mozilla/5.0 (windows; u; msie 9.0; windows nt 9.0; en-us)",
 "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; win64; x64; trident/5.0; .net clr 3.5.30729; .net clr 3.0.30729; .net clr 2.0.50727; media center pc 6.0)",
 "mozilla/5.0 (compatible; msie 8.0; windows nt 6.0; trident/4.0; wow64; trident/4.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; .net clr 1.0.3705; .net clr 1.1.4322)",
 "mozilla/4.0 (compatible; msie 7.0b; windows nt 5.2; .net clr 1.1.4322; .net clr 2.0.50727; infopath.2; .net clr 3.0.04506.30)",
 "mozilla/5.0 (windows; u; windows nt 5.1; zh-cn) applewebkit/523.15 (khtml, like gecko, safari/419.3) arora/0.3 (change: 287 c9dfb30)",
 "mozilla/5.0 (x11; u; linux; en-us) applewebkit/527+ (khtml, like gecko, safari/419.3) arora/0.6",
 "mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1.2pre) gecko/20070215 k-ninja/2.1.1",
 "mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9) gecko/20080705 firefox/3.0 kapiko/3.0",
 "mozilla/5.0 (x11; linux i686; u;) gecko/20070322 kazehakase/0.4.5",
 "mozilla/5.0 (x11; u; linux i686; en-us; rv:1.9.0.8) gecko fedora/1.9.0.8-1.fc10 kazehakase/0.5.6",
 "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/535.11 (khtml, like gecko) chrome/17.0.963.56 safari/535.11",
 "mozilla/5.0 (macintosh; intel mac os x 10_7_3) applewebkit/535.20 (khtml, like gecko) chrome/19.0.1036.7 safari/535.20",
 "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 - encoding': 'gzip, deflate',
    'accept - language': 'zh - cn, zh;q = 0.9',
    }
 conn = http.client.httpconnection("ttty.com", timeout=10)

 conn.request("get", "/thread0806.php?fid=16&"+page, params, headers)

 r1 = conn.getresponse()

 data = r1.read().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

以上这篇对python3标准库httpclient的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。