请教各位,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
其实这个问题是因为我自己没有注意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]]])
看,手册!!!!!!!!!!!!!!!!!!!!!!!!!
conn.close()
--------------------------------------------------------
没注意你用json的方式提交的,不过不影响,给你改改.......错误出在httpconnection那块了,不要带http://这样的东西
{"state":-1,"msg":"登录信息为空"}返回的时候数据是这样的,不知道是不是data设置不对!我之前都是直接用urllib2,还是比http更方便的。
供参考:
Python专题教程:抓取网站,模拟登陆,抓取动态网页
中的:
【教程】模拟登陆网站之Python版(内含两种版本的完整的可运行的代码)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。