Python urllib2发布到sms网关[Errno 10054]

Python urllib2发布到sms网关[Errno 10054],python,url,http-headers,http-post,sms-gateway,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_

我试图通过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
但是,如果我在web浏览器中复制合成的url(使用
print http_req
), 我在目的地号码(
dest
)收到一条短信(
message
上面代码中的值)。url类似于:

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

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

该错误通常是在远程主机未按预期格式接收数据并重置连接时引起的。(其他因素可能会导致此错误,例如网络链路故障或远程主机上出现问题,但在您的场景中似乎不太可能。)

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


wireshark可能不适合您,这取决于数据包的传输距离。在这种情况下,您可以尝试使用

可能是用户代理…,使用fiddler或您自己的web浏览器查看python请求和“手工”请求之间的差异。请您解释如何在web浏览器中查看“手工”请求?这取决于您使用的浏览器:在Chrome中按F12打开开发工具,按F5重新加载页面并检查网络选项卡