开发者社区> 问答> 正文

python post提交到服务器登陆,使用json数据,为什么不成功呢?? 400 报错

python post提交到服务器登陆,使用json数据,为什么不成功呢?? 400 报错

请教各位,python 用post请求登陆,使用的是json,没弄明白!


import httplib
import json
import urllib

conn = httplib.HTTPConnection("http://192.168.1.104:8080")
headers = {"Content-type":"application/json"}
param = ({"type":"2","loginName":"13983643100","password":"6aa5e3cc0678b335a9547f60045baa6b","systemVersion":"6.1.2"})

conn.request("POST" ,"/linkup_base/user/login.mlk",json.JSONEncoder().encode(param), headers)

response = conn.getresponse()
data = response.read(200000)
print(data)

conn.close()
 刚刚学习python,提交到服务器报错,如下 
Traceback (most recent call last):
  File "C:/Documents and Settings/Administrator/PycharmProjects/untitled/login.py", line 12, in <module>
    conn.request("POST" ,"/linkup_base/user/login.mlk?",postData, headers)
  File "C:\Python27\lib\httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 829, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 791, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 772, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

展开
收起
爱吃鱼的程序员 2020-06-05 12:49:35 1001 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    其实这个问题是因为我自己没有注意post请求,下面是代码

    import json
    import urllib2,urllib
    params = urllib.urlencode({'param': json.dumps({'type':'2','loginName':'13983643158','password':'6aa5e3cc0678b335a9547f60045baa6b','systemVersion':'6.1.2'})})
    print params
    f = urllib.urlopen("http://192.168.1.104:8080/line/user/login.mlk",params)
    data= f.read()
    print(data)
    ddata=json.loads(data)
    print(ddata)
    type(ddata)
    print(ddata['data'])

    ######

    httplib.HTTPConnection ( host [ , port [ , strict [ , timeout ]]] ) 

    看,手册!!!!!!!!!!!!!!!!!!!!!!!!!


    ######回复 @geminiblue : 请教下呢,调试了下,解析不到json######HTTPConnection.request ( method , url [ , body [ , headers ]] )######socket.gaierror: [Errno 11001] getaddrinfo failed 开错误代码,那就是说明你请求的地址都解析失败了 不是给你发上去吗.httplib.HTTPConnection挂掉了######request = urllib2.Request(posturl, postData, headers) 这样不行?######指教下呢,######import httplib
    import json
    import urllib
    try:
        conn = httplib.HTTPConnection("127.0.0.1",8080)
    except Exception,e:
        print e
    headers = {"Content-Type":"application/x-www-form-urlencoded"}
    param = ({"type":"2","loginName":"13983643100","password":"6aa5e3cc0678b335a9547f60045baa6b","systemVersion":"6.1.2"})
    conn.request("POST" ,"/index.php/site/index", urllib.urlencode(param), headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
    ###### import httplib 
    import json 
    import urllib 
    try: 
        conn = httplib.HTTPConnection("127.0.0.1",8080) 
    except Exception,e: 
        print e 
    headers = {"Content-Type":"application/json"} 
    param = ({"type":"2","loginName":"13983643100","password":"6aa5e3cc0678b335a9547f60045baa6b","systemVersion":"6.1.2"})
    conn.request("POST" ,"/index.php/site/index", json.JSONEncoder().encode(param), headers) 
    response = conn.getresponse() 
    data = response.read() 
    print(data) 

    conn.close() 

    --------------------------------------------------------

    没注意你用json的方式提交的,不过不影响,给你改改.......错误出在httpconnection那块了,不要带http://这样的东西

    ######{"state":-1,"msg":"登录信息为空"} 返回的时候数据是这样的,不知道是不是data设置不对!######

    我之前都是直接用urllib2,还是比http更方便的。


    供参考:

    ######谢谢。
    2020-06-05 12:49:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载