各位大佬好:
我使用conn.request(method="POST","post.php",body=params,headers=headers)发送请求时,报错:
SyntaxError: non-keyword arg after keyword arg
下面是源代码:
import httplib,urllib
#请求传输的内容
params =urllib.urlencode({'memberInfoPart.memberLoginName':'abc@163.com','memberInfoPart.memberLoginPw':'aaaaaaaa','rememberLoginName':'on'})
#头部信息
headers={"Host":"www.163.com","User-Agent":"Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3","Accept-Encoding":"gzip, deflate","Referer":"http://www.163.com/163_passport/login.do?return_url=http%3A%2F%2Fwww.163.com/FindexAction_index.do","Connection":"keep-alive","Content-Type":"application/x-www-form-urlencoded","Content-Length":"185"}
#进行连接
conn =httplib.HTTPConnection("http://www.163.com/",80)
#发送请求
conn.request(method="POST",url="163_passport/login.do?",body=params,headers=headers)
提示错误 :
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
conn.request(method="POST",url="163_passport/login.do?",body=params,headers=headers)
File "D:\Program Files\python\lib\httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "D:\Program Files\python\lib\httplib.py", line 986, in _send_request
self.putrequest(method, url, **skips)
File "D:\Program Files\python\lib\httplib.py", line 856, in putrequest
raise CannotSendRequest()
CannotSendRequest
把上面的发送请求conn.request(method="POST",url="163_passport/login.do?",body=params,headers=headers)改为
conn.request(method="POST",url="163_passport/login.do?",params,headers)
提示报下面的错误
SyntaxError: non-keyword arg after keyword arg
SyntaxError:non-keywordargafterkeywordarg
因为 conn.request(method="POST",url="163_passport/login.do?",params,headers)前面两个是关键字参数,函数调用规定当混合使用关键字参数和非关键字参数时,非关键字参数要在前面
从异常来看,貌似是连接在request前就中断了,建议直接翻下源码看看
试下conn.request(method="POST",url="/post.php",body=params,headers=headers)
要不就改成 conn.request("POST","/post.php",params,headers)
看下这篇文章吧,关于args和kwargs
http://jasonwu.me/2012/12/03/args_and_kwargs_in_python.html
上面兄台,我试过,还是报错误,感谢你的帮助,我在查一查相关文档conn=httplib.HTTPConnection("http://www.163.com/",80)
这个里面的内容 不能带http
SyntaxError:non-keywordargafterkeywordarg
因为 conn.request(method="POST",url="163_passport/login.do?",params,headers)前面两个是关键字参数,函数调用规定当混合使用关键字参数和非关键字参数时,非关键字参数要在前面
从异常来看,貌似是连接在request前就中断了,建议直接翻下源码看看
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。