requestsモジュール03——シミュレーションはmultiiprt/form-dataフォーマットを通してpost要求を送ります.

3450 ワード

前言:晴れた日ごとに、あなたが笑えるように.
方式一:OrderedDictモジュールの導入
from collections import OrderedDict

headerinfos_01 = {
    'Accept':'application/json, text/javascript, */*; q=0.01',
    'X-Requested-With':'XMLHttpRequest',
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
}

get_params_03 = { "c":"post", "a":"doadd", "_json":"1", "fid":"80" } from_data1 = OrderedDict( [ ("atc_title", (None, 'newdream1111')), ("atc_content", (None, 'newdream1111')), ('pid', (None, '')), ('tid', (None, '')), ('special', (None, 'default')), ('reply_notice', (None, '1')), ('csrf_token', (None, value)) ] ) #OrderedDict     
 response04 = session_req.post( url='http://47.107.178.45/phpwind/index.php', #   params = get_params_03, headers = headerinfos, files = from_data1 ) print( response04.content.decode('utf-8') )
方式二:
1.multiiprtformdata類を導入する
class MultipartFormData(object):
    """multipart/form-data    """

    @staticmethod
    def format(data, boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW", headers={}):
        """
        form data
        :param: data:  {"req":{"cno":"18990876","flag":"Y"},"ts":1,"sig":1,"v": 2.0}
        :param: boundary: "----WebKitFormBoundary7MA4YWxkTrZu0gW"
        :param: headers:   boundary    ;  boundary headers     headers  
        :return: str
        :rtype: str
        """
        #  headers   boundary  
        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
Content-Disposition: form-data; name="{}"\r
\r
{}\r
' 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
2.headerに追加
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryCBnR4ejvQPQNsCEC'
3.オブジェクトの作成
m = MultipartFormData.format(data=data, headers=headers_info)
4.最後の引用
session_03.post(url='http://47.107.178.45/phpwind/index.php', data=m, params=para_03,cookies =cookie,headers=headers_info)