Python urllib2 POST到sms网关[Errno 10054]

时间:2016-03-30 14:22:08

标签: python url http-headers http-post sms-gateway

我试图通过Python脚本调用SMS网关。我按如下方式构建了一个http请求:

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), 我在目的地号码(message)收到短信(上面代码中的dest值)。网址类似于:

https://smsgw.com/gateway.ashx?producttoken=c98f7b71-xyz23-4429-9daa-9647&body=SMS%20Gateway%20Test%20message&to=00331234567&from=MyApp

这与我从Python发送请求的事实有关吗?是否有任何解决方法,例如通过在帖子中添加标题?

1 个答案:

答案 0 :(得分:0)

当远程主机没有以预期的格式接收数据并重置连接时,通常会导致该错误。 (其他因素可能导致此错误,例如网络链接失败或远程主机出现问题,但在您的方案中似乎不太可能。)

您可以尝试使用wireshark来确定发送的实际数据包之间的差异并相应地修改您的脚本。

有可能wireshark可能不适合您,具体取决于数据包的距离。在这种情况下,您可以尝试使用Service Trace Viewer