python解析http包併發送

 複製fiddler等抓包軟件中的請求數據,可使用下面代碼直接重發

#coding:utf8

import requests,urllib3
import sys, os
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#解析http協議使用requests發送


def sender(content, allowredirect=True):
  method = 'get'
  url = ''
  headers = {}
  correct = True
  data = ''
  lines = content.strip().split('\n')
  for key,line in enumerate(lines):
    if key == 0:
      tmp = line.strip().split()
      if len(tmp) != 3:
        print('第一行錯誤 '+line)
        correct = False
        break
      method = tmp[0].lower()
      url = tmp[1]
    elif method == 'post' and key == len(lines)-1:
      data = line.strip()
    elif line:
      tmp = line.strip().split(':')
      if len(tmp) < 2:
        correct = False
        print('headers error '+line)
        break
      tmp[1] = tmp[1].strip()
      headers[tmp[0].lower()] = ':'.join(tmp[1:])

  if correct:
    if not url.startswith('http:') and not url.startswith('https:'):
      url = 'http://'+headers.get('host')+url
    
    return requests.request(**{
      'url':url, 
      'data':data,
      'verify':False, 
      'method':method,
      'headers':headers,
      'allow_redirects':allowredirect,
      #'proxies':{"http": "http://127.0.0.1:8888", "https": "http://127.0.0.1:8888"}
    })


# if not os.path.isfile(sys.argv[1]):
#   print('從文件中讀取並解析')
#   sys.exit()
# content = ''
# with open(sys.argv[1]) as f:
#   content = f.read()
# if not content:
#   print('文件錯誤')


content = '''
POST https://mcs.snssdk.com/v1/list HTTP/1.1
Host: mcs.snssdk.com
Connection: keep-alive
Content-Length: 2898
Origin: https://bytedance.feishu.cn
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Lark/3.19.3 Chrome/73.0.3683.119 Electron/5.0.0 Safari/537.36 LarkLocale/zh_CN
Content-Type: application/json; charset=UTF-8
Accept: */*
Referer: https://bytedance.feishu.cn/drive/home/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN

[{"header":{"app_id":1229,"app_name":"docs","os_name":"windows","os_version":"10","device_model":"windows","ab_version":"1381012,1151324,954767,954863","language":"zh-CN","app_language":"zh","platform":"web","sdk_version":"4.0.6","timezone":8,"tz_offset":-28800,"resolution":"1536x864","browser":"Chrome","browser_version":"73.0","referrer":"","referrer_host":"","headers":"{\"custom\":{\"screen_width\":1536,\"screen_height\":864,\"channel\":\"cn\",\"tea_version\":\"v2\",\"platform\":\"lark\",\"app_form\":\"\",\"browser\":\"Chrome\",\"browser_version\":\"73.0\",\"browser_kernel\":\"blink\",\"tenant_id\":\"52a1bb0839fd04b378c9f20acc386a6ca36aed0c\",\"department_id\":\"8a24de0608816b078378c7829927321cf2a51c59\",\"web_version_date\":\"3-13\",\"web_version_timestamp\":1584086246379,\"gray_scale\":\"master\",\"disable_auto_pv\":true}}"},"user":{"user_unique_id":"d38239dfb4e7d9c0eb9e9657434787d766954925","web_id":"6802378140359509511","ssid":"434f4345-b3fb-4c01-9ebc-d23b7b34f721"},"events":[{"event":"doc_request_send_start","params":"{\"_staging_flag\":0,\"uuid\":\"c2ba5bb4-e62b-43ea-9ff7-81fbee5f9868\",\"request_id\":\"OwXoi8FCGjQu-6755326066396496141\",\"x_command\":\"api.passport.heartbeat\",\"hostname\":\"\",\"timeout\":0,\"method\":\"get\",\"module\":\"home\",\"file_id\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"file_type\":\"\",\"loc_file_id\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"loc_file_type\":\"\",\"page_type\":\"drive\",\"page_token\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"user_type\":\"suite\",\"path\":\"/drive/home/\",\"session_id\":\"MzdnfjILx9Ztw4wTcOYP\",\"member_id\":\"\",\"file_tenant_id\":\"6cb9acc00ffb4016dd5fe3a5d98e0bc5b94c285e\",\"file_is_cross_tenant\":\"true\",\"jsHeapSizeLimit\":1090519040,\"totalJSHeapSize\":48013291,\"usedJSHeapSize\":43409291,\"event_index\":1584407166916}","local_time_ms":1584408909808,"is_bav":0},{"event":"doc_request_send_end","params":"{\"_staging_flag\":0,\"uuid\":\"c2ba5bb4-e62b-43ea-9ff7-81fbee5f9868\",\"request_id\":\"OwXoi8FCGjQu-6755326066396496141\",\"x_command\":\"https:..bytedance.feishu.cn.space.api.passport.heartbeat\",\"hostname\":\"bytedance.feishu.cn\",\"status_code\":200,\"responseType\":\"\",\"duration_time\":76,\"code\":0,\"status\":\"success\",\"module\":\"home\",\"file_id\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"file_type\":\"\",\"loc_file_id\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"loc_file_type\":\"\",\"page_type\":\"drive\",\"page_token\":\"3b88e1def00cff667f569116c6d0344d9efa686d\",\"user_type\":\"suite\",\"path\":\"/drive/home/\",\"session_id\":\"MzdnfjILx9Ztw4wTcOYP\",\"member_id\":\"\",\"file_tenant_id\":\"6cb9acc00ffb4016dd5fe3a5d98e0bc5b94c285e\",\"file_is_cross_tenant\":\"true\",\"jsHeapSizeLimit\":1090519040,\"totalJSHeapSize\":48014089,\"usedJSHeapSize\":43511213,\"event_index\":1584407166917}","local_time_ms":1584408909885,"is_bav":0}]}]
'''
print(sender(content).status_code)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章