使用httplib.httpConnection连接报错

Love_Python 发布于 2013/06/25 09:21
阅读 3K+
收藏 0

各位大佬好:

    我使用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

以下是问题补充:

@Love_Python:我将conn.request(method="POST","post.php",body=params,headers=headers) 改为conn.request("POST","post.php",params,headers) 又报下面的错误 Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> conn.request("POST","post.php",params,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 (2013/06/25 10:01)
加载中
0
_yjp
_yjp

SyntaxError: non-keyword arg after keyword arg

因为conn.request(method="POST",url="163_passport/login.do?",params,headers)前面两个是关键字参数,函数调用规定当混合使用关键字参数和非关键字参数时,非关键字参数要在前面

从异常来看,貌似是连接在request前就中断了,建议直接翻下源码看看


0
Pongox
Pongox

试下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

0
Love_Python
Love_Python
上面兄台,我试过,还是报错误,感谢你的帮助 ,我在查一查相关文档
0
请叫我周润发
请叫我周润发

conn =httplib.HTTPConnection("http://www.163.com/",80)

这个里面的内容  不能带http

0
迷你鲨鱼
迷你鲨鱼

引用来自“_yjp”的答案

SyntaxError: non-keyword arg after keyword arg

因为conn.request(method="POST",url="163_passport/login.do?",params,headers)前面两个是关键字参数,函数调用规定当混合使用关键字参数和非关键字参数时,非关键字参数要在前面

从异常来看,貌似是连接在request前就中断了,建议直接翻下源码看看


。。。

Python函数中,带默认值的函数必须在没有默认值参数的后面

返回顶部
顶部