Python urllib2 POST to an sms gateway [Errno 10054]
I am trying to invoke an SMS gateway via a Python script. I build an http request as follows:
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). The url is something like:
Does it have anything to do with the fact that I send the request from 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.
It's possible wireshark might not work for you depending on how far the packet is getting. In that case, you can try using Service Trace Viewer.