我已经在Ubuntu中使用USB调制解调器设置了Kannel,我可以通过浏览器使用如下所示的URL发送短信
localhost:13013/cgi-bin/sendsms?username=kannel&password=kannel&to=+254781923855&text='Kid got swag'
在python中,我有以下脚本,它只在要发送的消息没有空格的情况下有效。在
^{pr2}$如果我在消息中使用NO空格来调用函数,它会工作,消息就会被发送。在
sms.send_sms('+254781923855', 'kid_got_swag')
如果消息中有空格,它将失败并返回错误belw
sms.send_sms('+254781923855', 'kid got swag')
Traceback (most recent call last):
File "/home/lukik/workspace/projx/src/short_message.py", line 24, in <module>
sms.send_sms('+254781923855', 'kid got swag')
File "/home/lukik/workspace/projx/src/short_message.py", line 18, in send_sms
f = urllib.request.urlopen(url)
File "/usr/lib/python3.2/urllib/request.py", line 139, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.2/urllib/request.py", line 376, in open
response = meth(req, response)
File "/usr/lib/python3.2/urllib/request.py", line 488, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.2/urllib/request.py", line 414, in error
return self._call_chain(*args)
File "/usr/lib/python3.2/urllib/request.py", line 348, in _call_chain
result = func(*args)
File "/usr/lib/python3.2/urllib/request.py", line 496, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
我尝试过调用urllib的其他变体,但是它们都失败了,因为消息中的空格。。。。在
在您通过浏览器发送的请求中,消息包含引号-
在你的请求中试试-
^{pr2}$注意
&text='%s'
处的单引号。在PS:我建议对这样的请求使用requests。你可以用这种方式更好地构建你的网址,就像这样-
^{3}$不允许URL包含空格。当您在浏览器中尝试时,浏览器会在发出请求之前对URL进行正确编码。在你的程序中,你需要对网址进行编码。幸运的是,
urllib
有内置函数来处理细节。在http://docs.python.org/3.3/library/urllib.parse.html#url-quoting
{a1请求的参数将失败,否则将无法通过构造的参数。我相信urllib.parse.urlencode能满足你的需要。在
相关问题 更多 >
编程相关推荐