python multipart/form-data post接口請求

python multipart/form-data post接口請求

def WebKit_format(data, boundary="----WebKitFormBoundary*********ABC", headers=None): # 從headers中提取boundary信息
        if headers is None: headers = {} if "content-type" in headers: fd_val = str(headers["content-type"]) if "boundary" in fd_val: fd_val = fd_val.split(";")[1].strip() boundary = fd_val.split("=")[1].strip() else: raise Exception("multipart/form-data頭信息錯誤,請檢查content-type key是否包含boundary") # form-data格式定式
        jion_str = '--{}\r\nContent-Disposition: form-data; name="{}"\r\n\r\n{}\r\n' end_str = "--{}--".format(boundary) args_str = ""
        if not isinstance(data, dict): raise Exception("multipart/form-data參數錯誤,data參數應爲dict類型") for key, value in data.items(): args_str = args_str + jion_str.format(boundary, key, value) args_str = args_str + end_str.format(boundary) args_str = args_str.replace("\'", "\"") return args_str 

def test001_auth_login(): headers = {'Accept': "application/json", 'Content-Type': "multipart/form-data; boundary=----WebKitFormBoundary*********ABC",
'Accept-Encoding': "gzip, deflate, br", 'Connection': "keep-alive"} url = "https://www.baidu.com/login/" bodyData = { "username": 1234567890, "password": "a123456", "type": "account", "bindType": "null", "openId": "null" } boundary_body = WebKit_format(data=bodyData, headers=headers) print(boundary_body) response = requests.post(url, data = boundary_body, headers = headers) access_token = regSearchString(response.text, '"access_token":"(.+?)",', 16, -2) print("正則--獲取登錄的access_token: " + access_token) print(response.text) time.sleep(1)

boundary_body = WebKit_format(data=bodyData, headers=headers)html

 轉換後的boundary_body 相似下面:python

------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="v"

2.0
------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="req" {"cno": "1213058673616305"} ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="sig"

1
------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="ts"

1
------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="appid" dp3wY4YtycajNEz23zZpb5Jl ------WebKitFormBoundary7MA4YWxkTrZu0gW--

 

 參考文章json

http://www.javashuo.com/article/p-rfmfxnck-bx.htmlapp

https://www.jianshu.com/p/0023bb7afddbpost

 

 

 

 

 

 

 

print

boundary_body = WebKit_format(data=bodyData, headers=headers)url

相關文章
相關標籤/搜索