Python:发送国际号码短信的脚本?

2024-04-26 02:24:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用下面的脚本发送短信,但我总是收到错误消息“检查您的互联网连接”。对于waytosms或任何其他服务,是否有可用的python脚本,我可以用它来发送国际号码的短信。

#!/usr/bin/python

import cookielib
import urllib2
from getpass import getpass
import sys
from urllib import urlencode
from getopt import getopt

ask_username = True
ask_password = True
ask_message = True
ask_number = True

def Usage():
    print '\t-h, --help:  View help'
    print '\t-u, --username: Username'
    print '\t-p, --password: Password'
    print '\t-n, --number: numbber to send the sms'
    print '\t-m, --message: Message to send'
    sys.exit(1)


opts, args = getopt(sys.argv[1:], 'u:p:m:n:h',["username=","password=","message=","number=","help"])

for o,v in opts:
    if o in ("-h", "--help"):
        Usage()
    elif o in ("-u", "--username"):
        username = v
        ask_username = False
    elif o in ("-p", "--password"):
        passwd = v
        ask_password = False
    elif o in ("-m", "--message"):
        message = v
        ask_message = False
    elif o in ("-n", "--number"):
        number = v
        ask_number = False

#Credentials taken here
if ask_username: username = raw_input("Enter USERNAME: ")
if ask_password: passwd = getpass()
if ask_message: message = raw_input("Enter Message: ")
if ask_number: number = raw_input("Enter Mobile number: ")

#Logging into the SMS Site
url = 'http://wwwg.way2sms.com//auth.cl'
data = 'username='+username+'&password='+passwd+'&Submit=Sign+in'

#Remember, Cookies are to be handled
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# To fool way2sms as if a Web browser is visiting the site
opener.addheaders = [('User-Agent','Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3 GTB7.0')]
try:
    usock = opener.open(url, data)
except IOError:
    print "Check your internet connection"
    sys.exit(1)

#urlencode performed.. Because it was done by the site as i checked through HTTP headers

message = urlencode({'message':message})
message = message[message.find("=")+1:]

#SMS sending
send_sms_url = 'http://wwwg.way2sms.com//FirstServletsms?custid='
#Check this line with HTTP Headers, if script is not working
send_sms_data = 'custid=undefined&HiddenAction=instantsms&Action=custfrom950000&login=&pass=&MobNo='+number+'&textArea='+message
opener.addheaders = [('Referer','http://wwwg.way2sms.com//jsp/InstantSMS.jsp?val=0')]

try:
    sms_sent_page = opener.open(send_sms_url,send_sms_data)
    inp = open("log.html","w")
    inp.write(sms_sent_page.read())
    inp.close()

except IOError:
    print "Check your internet connection( while sending sms)"
    sys.exit(1)
print "SMS sent!!!"

请建议我的短信服务,我也可以得到python脚本。


Tags: inimportsendtruenumbermessageifsys
3条回答

Twiliohighly recommended

此代码使用Twilio发送文本消息:

from twilio.rest import TwilioRestClient

account = "getFromYourTwilioDashboard"
token   = "getFromYourTwilioDashboard"
client  = TwilioRestClient(account, token)

message = client.sms.messages.create(to="+yourCellNumber", from_="+yourTwilioNumber", body="Message here")

这不是免费的,但易于使用和可靠。

日常用品不见了。脚本已更新为使用Solinked:http://pastebin.com/21yft50r

但请不要过度使用,否则它们也会关闭。

您可以试试twilio,下面的链接中还提供了正式的python代码。 但这不是免费服务,所以您可以看到pricing

https://github.com/twilio/twilio-python#readme

相关问题 更多 >