开发者社区> 问答> 正文

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

请教各位,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-22 18:16:43 798 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

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

    importjsonimporturllib2,urllibparams=urllib.urlencode({'param':json.dumps({'type':'2','loginName':'13983643158','password':'6aa5e3cc0678b335a9547f60045baa6b','systemVersion':'6.1.2'})})printparamsf=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:请教下呢,调试了下,解析不到jsonHTTPConnection.request(method,url[,body[,headers]])socket.gaierror:[Errno11001]getaddrinfofailed开错误代码,那就是说明你请求的地址都解析失败了不是给你发上去吗.httplib.HTTPConnection挂掉了request=urllib2.Request(posturl,postData,headers)这样不行?指教下呢,importhttplib
    importjson
    importurllib
    try:
      conn=httplib.HTTPConnection("127.0.0.1",8080)
    exceptException,e:
      printe
    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()
    importhttplib 
    importjson 
    importurllib 
    try: 
      conn=httplib.HTTPConnection("127.0.0.1",8080) 
    exceptException,e: 
      printe 
    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更方便的。


    供参考:

    Python专题教程:抓取网站,模拟登陆,抓取动态网页


    中的:

    【教程】模拟登陆网站之Python版(内含两种版本的完整的可运行的代码)


    谢谢。
    2020-06-22 18:17:02
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS快储存加密技术 立即下载