0
我想通过Python脚本调用SMS网关。我建立一个HTTP请求如下:Python urllib2 POST到短信网关[Errno 10054]
http_req = url
http_req += '?'
http_req += 'producttoken='+token
http_req += '&msg='+urllib.quote(message)
http_req += '&to='+dest
http_req += '&from='+sender
然后我发送一个请求:
req = urllib2.Request(http_req)
try:
response = urllib2.urlopen(req)
response_url = response.geturl()
print response_url
if response_url==url:
print 'SMS sent!'
except urllib2.URLError, e:
print 'Send failed!'
print e.reason
结果,我得到:
Send failed!
[Errno 10054] An existing connection was forcibly closed by the remote host
但是,如果我复制组成网址(在网页浏览器中使用print http_req
), 我在目标号码(dest
)处收到一条SMS(上面代码中的message
值)。网址是这样的:
https://smsgw.com/gateway.ashx?producttoken=c98f7b71-xyz23-4429-9daa-9647&body=SMS%20Gateway%20Test%20message&to=00331234567&from=MyApp
这是否与我从Python发送请求有什么关系?是否有任何解决方法,例如通过向帖子添加标题?
也许用户代理...,使用您自己的提琴手或网页浏览器来查看您的python请求和“手工制作”请求之间的区别。 – Goufalite
您能解释一下如何在网页浏览器中看到“手工制作”的请求吗? – QuestionMark
这取决于您使用的浏览器:在Chrome中按F12打开developper工具,按F5重新加载页面并检查网络选项卡 – Goufalite