簡單/非阻塞 的 HTTP 服務庫:simple_http

jopen 10年前發布 | 44K 次閱讀 Python開發 simple_http

一個簡單好用的HTTP庫, 支持標準的http方法

返回頭部

header["status"] 是狀態碼
header["message"] 是message
header["protocol"] 是協議
它們跟其它頭混到在一個字典里, 因為沒有區分的必要

使用不同的header

默認情況下simple_http使用firefox的User-Agent, 請注意此庫不會把參數里的其它類型自動轉化為str, 用其它類型會直接報錯

myheader = {     "Accept": ...} simple_http.get("https://google.com", header=myheader) 

使用Cookie

cookie = {     "name": "value" } simple_http.get("https://github.com", cookie=cookie) 

從header里取Cookie用

simple_http.client_cookie(header["Set-Cookie"]){' path': '/', ' secure': True, 'cookie': '_gh_sess=eyJzZXNzaW9uX2lkIjoiYTU5YTVhMmNjMTE1M2Y2ODU5MDczNjlmNGMzYWVmY2YiLCJfY3NyZl90b2tlbiI6IlVRUy8wdjkycnFhL2R0SGk1NVlkaDQ4d0lnSmljUEYwQzNOSWlGaG50bjQ9In0%3D--177119b094b3292c35f1573c8bd18a41fe8807ef', ' HttpOnly': True} 轉換到{     "_gh_sess": "...." } 

GET請求添加參數

query = {     "params": "value" } simple_http.get("https://google.com", query=query) 

POST添加參數

payload = {     "params": "value" } simple_http.post("https://google.com", payload=payload) 

POST里使用文件

payload = {     "name": open("test", "r") } simple_http.post("https://google.com", payload=payload) 

使用代理 HTTP and SOCKS5

Socks5

In [8]: simple_http.get("https://google.com", proxy='socks5://127.0.0.1:8888') Out[8]: ({'Alternate-Protocol': '443:quic',
  'Cache-Control': 'public, max-age=2592000',
  'Content-Length': '220',
  'Content-Type': 'text/html; charset=UTF-8',
  'Date': 'Wed, 08 Jan 2014 13:28:59 GMT',
  'Expires': 'Fri, 07 Feb 2014 13:28:59 GMT',
  'Location': 'https://www.google.com/',
  'Server': 'gws',
  'X-Frame-Options': 'SAMEORIGIN',
  'X-XSS-Protection': '1; mode=block',
  'message': 'Moved Permanently',
  'protocol': 'HTTP/1.1',
  'status': 301},
 None,
 '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A ) 

http代理

In [46]: simple_http.get("https://google.com", proxy='http://127.0.0.1:8088') Out[46]: ({'Alternate-Protocol': '443:quic',
  'Cache-Control': 'public, max-age=2592000',
  'Content-Encoding': 'deflate',
  'Content-Length': '172',
  'Content-Type': 'text/html; charset=UTF-8',
  'Date': 'Wed, 08 Jan 2014 13:46:57 GMT',
  'Expires': 'Fri, 07 Feb 2014 13:46:57 GMT',
  'Location': 'https://www.google.com/',
  'Server': 'gws',
  'Via': 'HTTP/1.1 GWA',
  'X-Frame-Options': 'SAMEORIGIN',
  'X-Xss-Protection': '1; mode=block',
  'message': '',
  'protocol': 'HTTP/1.1',
  'status': 301},
 None,
 '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A ) 

pretty.py是格式化HTML的工具, 它并不處理js與css, 主要為了澄清文檔結構

python pretty.py input.html > ouput.html

etree_utils.py是用于快速確定xpath的工具

因為瀏覽器會動態修改DOM,從源代碼界面取得xpath經常不能用.
常見的Beautifulsoup效率非常低, 又經常有些奇怪的bug, lxml配合xpath才是抓取網頁內容的最佳方案
主要是輔助快速確定目標的xpath, 使用語法是
1. a 指tag選擇器
2. .ele 是類選擇器
3. #id 是ID選擇器
4. href="link", =號是屬性選擇器
5. >是行選擇器

示例

python etree_util.py htmlfile 語法

tag: a
, line: 379
, attrib: {'href': '/album/120712490', 'title': u'\xe8\x9d\xb6\xe6\x81\x8b\xe8\x8a\xb1'} , text: 目標
, xpath: /html/body/div/ul/li[25]/div/span[6]/a=============== tag: a
, line: 385
, attrib: {'href': 'javascript:;', 'class': 'btn btn-b play-selected-hook'} , text: 

, xpath: /html/body/div/div[2]/a[1] =============== tag: a
, line: 394
, attrib: {'href': 'javascript:;', 'class': 'btn btn-b add-selected-hook'} , text: 

, xpath: /html/body/div/div[2]/a[2] =============== tag: a
, line: 403
, attrib: {'href': 'javascript:;', 'class': 'btn btn-b collect-selected-hook'} , text: 

, xpath: /html/body/div/div[2]/a[3] =============== tag: a
, line: 412
, attrib: {'href': 'javascript:;', 'class': 'btn btn-b down-selected-hook'} , text: 

, xpath: /html/body/div/div[2]/a[4] 

encryped_client/server是SOCKS5轉發代理

  1. 通過python simple_table.py得到key
  2. 要使用先自己修改代碼里的端口與服務器地址等配置信息
  3. 兩者都采用異步非阻塞的高效實現, 在本機測試時cpu從來沒有過1%.
  4. 加密機制是隨機密碼表, 轉換效率非常高
  5. encrypted_server.py與key放到墻外, encrypted_client.py與key放到本地

http流解析

主要是為了測試客戶端與服務端, 將網絡流DUMP到文件,再解析

python http_stream_parser.py stream.file

http模擬器, 多進程并發模擬瀏覽器操作

$python http_request_simulator.py "weibo.com" ========= page done: weibo.com
timeout: 3s ========= url done: http://tp4.sinaimg.cn/1693146987/50/0/0
url done: http://tp4.sinaimg.cn/2669568935/50/0/0
url done: http://tp1.sinaimg.cn/1724196104/50/0/0
url done: http://tp4.sinaimg.cn/1774814087/50/0/0
url done: http://tp4.sinaimg.cn/1736031115/50/0/0
url done: http://tp3.sinaimg.cn/1707759510/50/0/0
url done: http://tp4.sinaimg.cn/2642032423/50/0/0
url done: http://tp4.sinaimg.cn/1730725935/50/0/0
url done: http://tp4.sinaimg.cn/1242716987/50/0/0
url done: http://tp4.sinaimg.cn/1488834727/50/0/0
url done: http://tp3.sinaimg.cn/1871599514/50/0/0
url done: http://tp4.sinaimg.cn/1727873155/50/0/0
url done: http://tp1.sinaimg.cn/1886832164/50/0/0
url done: http://tp1.sinaimg.cn/1589797232/50/0/0
url done: http://tp1.sinaimg.cn/2441302392/50/0/0
url done: http://img.t.sinajs.cn/t35/style/images/tlogin/botlogo.png
url done: http://rs.sinajs.cn/mini.gif?t=w1&uids=1707759510,1759023505,1867565545,1724196104,2669568935,2441302392,1871599514,2642032423,1192428237
url done: http://js.t.sinajs.cn/t35/miniblog/js/yunying_unlogin3.js?version=20131127172405
url done: http://js.t.sinajs.cn/t35/miniblog/js/lang_zh.js?version=20131127172405
url done: http://tp2.sinaimg.cn/1867565545/50/0/0
url done: http://js.t.sinajs.cn/t35/miniblog/static/js/sso.js?v=20131127172405
url done: http://tp2.sinaimg.cn/1192428237/50/0/0
url done: http://i1.sinaimg.cn/unipro/pub/suda_m_v629.js
url done: http://beacon.sina.com.cn/e.gif?noScript
url done: http://tp2.sinaimg.cn/1759023505/50/0/0
request done, kill 0 children

項目主頁:http://www.baiduhome.net/lib/view/home/1414725473934

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!