簡體   English   中英

Python urllib2 POST到短信網關[Errno 10054]

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

我正在嘗試通過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

但是,如果我在網絡瀏覽器中復制了組成的url(使用print http_req ), 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發送請求有關嗎? 有沒有解決方法,例如通過在帖子中添加標題?

該錯誤通常是由於遠程主機未按預期格式接收數據並重置連接而引起的。 (其他原因也可能導致此錯誤,例如網絡鏈接失敗或遠程主機上的問題,但在您的情況下似乎不太可能。)

您可以嘗試使用Wireshark確定發送的實際數據包之間的差異,並相應地修改腳本。

根據數據包的傳輸距離,wireshark可能對您不起作用。 在這種情況下,您可以嘗試使用Service Trace Viewer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM