简体   繁体   English

Python urllib2 POST到短信网关[Errno 10054]

[英]Python urllib2 POST to an sms gateway [Errno 10054]

I am trying to invoke an SMS gateway via a Python script. 我正在尝试通过Python脚本调用SMS网关。 I build an http request as follows: 我建立一个http请求,如下所示:

http_req = url
http_req += '?'
http_req += 'producttoken='+token
http_req += '&msg='+urllib.quote(message)
http_req += '&to='+dest
http_req += '&from='+sender

Then I send a request: 然后我发送一个请求:

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

As the result, I receive: 结果,我收到:

Send failed!
[Errno 10054] An existing connection was forcibly closed by the remote host

However, If I copy the composed url (using print http_req ) in a web browser, I receive an SMS ( message value in the code above) at destination number ( dest ). 但是,如果我在网络浏览器中复制了组成的url(使用print http_req ), print http_req在目标号码( dest )收到一条SMS(上面代码中的message值)。 The url is something like: 网址类似于:

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

Does it have anything to do with the fact that I send the request from Python? 这与我从Python发送请求有关吗? Is there any workaround, for example by adding a header to the post? 有没有解决方法,例如通过在帖子中添加标题?

That error is generally caused when the remote host doesn't receive data in the format it expects and resets the connection. 该错误通常是由于远程主机未按预期格式接收数据并重置连接而引起的。 (Other things can cause this error, such as a failed network link or problems on the remote host, but they seem unlikely in your scenario.) (其他原因也可能导致此错误,例如网络链接失败或远程主机上的问题,但在您的情况下似乎不太可能。)

You can try to use wireshark to determine the difference between the actual packets sent and modify your script accordingly. 您可以尝试使用Wireshark确定发送的实际数据包之间的差异,并相应地修改脚本。

It's possible wireshark might not work for you depending on how far the packet is getting. 根据数据包的传输距离,wireshark可能对您不起作用。 In that case, you can try using Service Trace Viewer . 在这种情况下,您可以尝试使用Service Trace Viewer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM