问答详情
源自:4-2 接口测试工具-python-post接口实战

为什么我的还是说提交格式不正确,有人可以看出来为什么么?

#定义代码格式
#-*- coding:utf8 -*-
from urllib import request
from urllib import parse
from urllib.request import urlopen
url = "http://xapi.kybyun.com/user/login"
#定义请求数据并赋值
headers = {}
headers = {
        'Host': 'xapi.kybyun.com',
        'Connection': 'keep-alive',
        'User-Agent': 'BangXueTang AipBot/1.0(BangXueTang-IOS/2.1.3.1;IOS/9.30;iPhone 6 Plus)',
        'KY-UKEY': '940cd0dffd371d41eb0acbb7694fd5e9',
        'KY-SYDEV': 'iPhone 6 Plus',
        'KY-SPEID': '10010101',
        'KY-SCHID': '1044',
        'KY-APPCHG': 'AppStore',
        'Connection': 'keep-alive',
        'KY-UUID': '6ff7563dbd47c8077905c3920bc0d8b3',
        'KY-YEAR': '2017',
        'Accept-Language': 'zh-Hans-CN;q=1',
        'KY-SYSVER': '9.3',
        'Accept': '*/*',
        'Content-Type': 'application/x-www-form-urlencoded',
        'KY-APPVER': '2.1.3.1',
        'KY-APPVERS': '4',
        'KY-APPTYPE': '21'
}
data = {}
data['appchg'] = 'Appstore'
data['apptype'] = '21'
data['appver'] = '2.1.3.1'
data['email'] = 'mushishi01'
data['isblind'] = '0'
data['passwd'] = '111111'
data['sysdev'] = 'iPhone 6 Plus'
data['sysver'] = '9.3'
data['uuid'] = '6ff7563dbd47c8077905c3920bc0d8b3'
#对数据进行编码
data = parse.urlencode(data).encode(encoding='UTF8')
#对post的数据进行合并
req = request.Request(url, data, headers)
#打开请求获取对象
requestResponse = urlopen(req)
#读取服务端返回的对象
responseStr = requestResponse.read()
#将密码进行转译(因为密码用的是MD5的,对应上边)
ResponseStr = responseStr.decode("unicode_escape")
#打印数据
print(ResponseStr)


提问者:weixin_慕盖茨6132621 2018-12-25 17:38

个回答

  • 慕田峪6579940
    2019-06-03 14:56:24

    Traceback (most recent call last):  File "E:\Python\Python37\lib\urllib\request.py", line 1317, in do_open    encode_chunked=req.has_header('Transfer-encoding'))  File "E:\Python\Python37\lib\http\client.py", line 1229, in request    self._send_request(method, url, body, headers, encode_chunked)  File "E:\Python\Python37\lib\http\client.py", line 1275, in _send_request    self.endheaders(body, encode_chunked=encode_chunked)  File "E:\Python\Python37\lib\http\client.py", line 1224, in endheaders    self._send_output(message_body, encode_chunked=encode_chunked)  File "E:\Python\Python37\lib\http\client.py", line 1016, in _send_output    self.send(msg)  File "E:\Python\Python37\lib\http\client.py", line 956, in send    self.connect()  File "E:\Python\Python37\lib\http\client.py", line 928, in connect    (self.host,self.port), self.timeout, self.source_address)  File "E:\Python\Python37\lib\socket.py", line 707, in create_connection    for res in getaddrinfo(host, port, 0, SOCK_STREAM):  File "E:\Python\Python37\lib\socket.py", line 748, in getaddrinfo    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):socket.gaierror: [Errno 11001] getaddrinfo failed


  • 慕虎2019268
    2019-04-19 00:49:51

    #定义代码格式

    #-*- coding:utf8 -*-

    from urllib import parse

    from urllib import request


    url = 'http://xapi.kybyun.com/user/login'

    #定义请求数据并赋值

    headers = {}

    headers = {

       'Host': 'xapi.kybyun.com',

       'Connection': 'keep-alive',

       'User-Agent': 'BangXueTang AipBot/1.0(BangXueTang-IOS/2.1.3.1;IOS/9.30;iPhone 6 Plus)',

       'KY-UKEY': '940cd0dffd371d41eb0acbb7694fd5e9',

       'KY-SYSDEV': 'iPhone 6 Plus',

       'KY-SPEID': '10010101',

       'KY-SCHID': '1044',

       'KY-APPCHG': 'AppStore',

       'Connection': 'keep-alive',

       'KY-UUID': '6ff7563dbd47c8077905c3920bc0d8b3',

       'KY-YEAR': '2017',

       'Accept-Language': 'zh-Hans-CN;q=1',

       'KY-SYSVER': '9.3',

       'Accept': '*/*',

       'Content-Type': 'application/x-www-form-urlencoded',

       'KY-APPVER': '2.1.3.1',

       'KY-APPVERS': '4',

       'KY-APPTYPE': '21'

    }

    data = {}

    data['appchg'] = 'AppStore'

    data['apptype'] = '21'

    data['appver'] = '2.1.3.1'

    data['email'] = 'mushishi01'

    data['isblind'] = '0'

    data['passwd'] = '111111'

    data['sysdev'] = 'iPhone 6 Plus'

    data['sysver'] = '9.3'

    data['uuid'] = '6ff7563dbd47c8077905c3920bc0d8b3'

    #数据编码以及赋值

    data = parse.urlencode(data).encode(encoding='utf-8')

    #对post的数据进行合并

    requestStr = request.Request(url,data,headers)

    #打开地址并且赋值给变量

    requestResponse = request.urlopen(requestStr)

    #读取获得的值

    responseStr = requestResponse.read()

    #将密码进行转译(因为密码用的是MD5的)

    responseStr = responseStr.decode('unicode_escape')

    print(responseStr)

    运行结果:

    {"errmsg":"","version":1,"state":11459522,"res":{"uid":"11459522","uname":"mushishi01","scores":"2556","isavatar":"1","ctime":"1468306120","phone":"","sex":"0","year":"2018","proid":"1000","cityid":"1006","schid":"1033","schname":"北京大学","preschid":"1038","preschname":"对外经济贸易大学","speid":"10101100","spename":"护理学","subcity":"北京 海淀区","isdone":"1","iskyb":"1","isbxt":"1","schids":"1033,1040,1425","schnames":"北京大学,中国人民大学,黑龙江大学","onpic":"http:\/\/cdn2.kybimg.com\/avatar\/011\/45\/95\/22_avatar_middle.jpg","subids":"10,50,60,301,402","subnames":"政治,管理类联考,法硕(非法学),数学一,英语二"}}



  • 慕仰3184003
    2019-04-02 19:45:32

    去大卖场额拿的7啦 1517几回去了 有元的

  • 慕村120960
    2019-03-30 00:28:24

    你的代码在python3里面是正确的

  • SecondChen
    2019-01-09 16:51:12

    data = parse.urlencode(data).encode(encoding='UTF8')   这一句代码错了吧