向手机发送短信的python源代码

bambooboy2001cn 2007-05-07 09:05:15

PRODUCT_INFO='Batch registration tool to add mail user and send short message.\r\n'

#SM profile

import os
if os.name=='nt':
WMPROFILE='c:/etc/webmail.pf'
SMPROFILE='c:/etc/smc.pf'
else:
WMPROFILE='/etc/webmail.pf'
SMPROFILE='/etc/smc.pf'


MAX_VALID_CODE=999999
MAIL_DELIMITER='\xFF'

EMAIL_MOBILE_NUMBER = 'emailmobilenumber'
EMAIL_MOBILE_CODE = 'emailmobilecode'
SYS_EMAIL_MOBILE_CODE = 'emailmobilecodesys'

SMS_NUMBER = 'SMS_NUMBER'
SMS_EMAIL_NOTIFY = 'SMS_EMAILNOTIFY'


import os
import sys
import string
import random


import srpc
import smlib
import smcic

IsDigits=smlib.IsDigits

# Logging support
# ===============

logfile = "" # Filename to log to, if not empty
logfp = None # File object to log to, if not None

def initlog(*allargs):
"""Write a log message, if there is a log file.

Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).

The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.

If the global logfp is not None, it should be a file object to
which log data is written.

If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode. This file should be
world writable!!! If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).

"""
global logfp, log
if logfile and not logfp:
try:
logfp = open(logfile, "a")
except IOError:
pass
if not logfp:
log = nolog
else:
log = dolog
apply(log, allargs)

def dolog(fmt, *args):
"""Write a log message to the log file. See initlog() for docs."""
logfp.write(fmt%args + "\n")

def nolog(*allargs):
"""Dummy function, assigned to log when logging is disabled."""
pass

log = initlog # The current logging function


#Get Mail Server
def getServer():
addr=smlib.getConfigStr(WMPROFILE,'NET','SERVER','127.0.0.1')
port=smlib.getConfigInt(WMPROFILE,'NET', 'PORT',9010)
return (addr,port)

#Get Sms Server
def getSMServer():
addr=smlib.getConfigStr(SMPROFILE,'setup','ServerIp','127.0.0.1')
port=smlib.getConfigInt(SMPROFILE,'setup','Port',8801)
return (addr,port)


def connectServer():
addr=getServer()
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return None
return cli

def connectSMServer():
addr=getSMServer()
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
return None
return cli


def BatchRegister():

global logfile, logfp
print PRODUCT_INFO

print 'Input file name:'
st=sys.stdin
file=st.readline()
#print file

file=string.strip(file)
if not os.path.exists( file) or not os.path.isfile( file):
print '%s is directory or does not exist.\r\n' % file
return

try:
fd=open(file,'r')
except IOError, err:
print err
print
return

head,tail=os.path.splitext(file)
logfile=head+'.err'
domain=smlib.getConfigStr(WMPROFILE,'domain','domain','zj165.com')


mail_clnt=connectServer()
if not isinstance(mail_clnt,srpc.SRPCClient):
fd.close()
print "Can't connect to mail server!\r\n"
return

sms_clnt=connectSMServer()
if not isinstance(sms_clnt,srpc.SRPCClient):
fd.close()
mail_clnt.Close()
print "Can't connect to sms server!\r\n"
return

ok=0
all=0
false=0
while(1):
line=fd.readline()
if not line:
break

line=string.strip(line)
if len(line)==0:
continue

print line
if not IsDigits(line):
all+=1
false+=1
log( '%s is not digits\n', line)
continue

rand="%06d" % random.randint(0,MAX_VALID_CODE)
userdef="%s%s%s" % ( line, MAIL_DELIMITER, rand)

retcode=smcic.call_add_user(mail_clnt,line,domain,userdef)
if len(retcode)!=1 or retcode[0]!=1 :
all+=1
false+=1
log( 'add mail user %s@%s fail, return %s !', line,domain,retcode)
continue

retcode=smcic.call_find_user(mail_clnt,line,domain)
if len(retcode)!=2 or retcode[0]!=1 :
log( 'find mail user %s@%s fail, return %s !', line,domain,retcode)

else:
uid=retcode[1]
smcic.call_set_userval( mail_clnt, uid, EMAIL_MOBILE_NUMBER, line)
smcic.call_set_userval( mail_clnt, uid, EMAIL_MOBILE_CODE, '1')
smcic.call_set_userval( mail_clnt, uid, SYS_EMAIL_MOBILE_CODE, '1')
smcic.call_set_userval( mail_clnt, uid, SMS_NUMBER, line)
smcic.call_set_userval( mail_clnt, uid, SMS_EMAIL_NOTIFY, '1')


msg='尊敬的用户,您的联通如意邮箱 %s@%s已经开通,将免费试用至5月31日,密码为 %s,详情请登录 mail.zj165.com.' % (line,domain,rand)
retcode=smlib.SendSingle(sms_clnt, line, msg)
if len(retcode)<1 or retcode[0]!=1 :
all+=1
false+=1
log( 'mail user %s@%s added, but send short message to %s fail, return %s !', line,domain,line,retcode)
continue

all+=1
ok+=1

fd.close()
if logfp:
logfp.close()

mail_clnt.Close()
sms_clnt.Close()

print '%d mobile number processed.' % all
print '%d mail user added, short message sent' % ok

if false>0:
print '%d mobile number process failed, error has been writen to %s.' %( false, logfile)


if __name__=='__main__':
BatchRegister()
print
print 'Press any key to exit...'

raw_input()
...全文
1616 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
smartstar2005 2008-07-16
  • 打赏
  • 举报
回复
Mark
wujilin 2008-04-15
  • 打赏
  • 举报
回复
我们公司的东西 怎么到这里来了
ChumpKlutz 2007-05-08
  • 打赏
  • 举报
回复
收藏之~~
bambooboy2001cn 2007-05-07
  • 打赏
  • 举报
回复
嗯,需要和运营商的短信网关联通才行,网关的IP、端口、帐号、密码,放在配置文件里面。
如何您需要可以去http://mail.trasin.net/netsoft 参考
iambic 2007-05-07
  • 打赏
  • 举报
回复
我问下,这个东西有什么用啊?是不是需要一个什么服务器?自己用不了吧?
bambooboy2001cn 2007-05-07
  • 打赏
  • 举报
回复

PRODUCT_INFO='Batch send given feeing short message tool.\r\n'

#SM profile

import os
if os.name=='nt':
WMPROFILE='c:/etc/webmail.pf'
SMPROFILE='c:/etc/smc.pf'
else:
WMPROFILE='/etc/webmail.pf'
SMPROFILE='/etc/smc.pf'

# Logging support
#=====================================================================

logfile = "" # Filename to log to, if not empty
logfp = None # File object to log to, if not None

def initlog(*allargs):
"""Write a log message, if there is a log file.

Even though this function is called initlog(), you should always
use log(); log is a variable that is set either to initlog
(initially), to dolog (once the log file has been opened), or to
nolog (when logging is disabled).

The first argument is a format string; the remaining arguments (if
any) are arguments to the % operator, so e.g.
log("%s: %s", "a", "b")
will write "a: b" to the log file, followed by a newline.

If the global logfp is not None, it should be a file object to
which log data is written.

If the global logfp is None, the global logfile may be a string
giving a filename to open, in append mode. This file should be
world writable!!! If the file can't be opened, logging is
silently disabled (since there is no safe place where we could
send an error message).

"""
global logfp, log
if logfile and not logfp:
try:
logfp = open(logfile, "w")
except IOError:
pass
if not logfp:
log = nolog
else:
log = dolog
apply(log, allargs)

def dolog(fmt, *args):
"""Write a log message to the log file. See initlog() for docs."""
logfp.write(fmt%args + "\n")

def nolog(*allargs):
"""Dummy function, assigned to log when logging is disabled."""
pass

log = initlog # The current logging function

#=====================================================================


import os
import sys
import time

import string

import srpc
import smlib


IsDigits=smlib.IsDigits

#Get sms server
def getSMServer():
addr=srpc.pf_getstr(SMPROFILE,'setup.serverip','127.0.0.1')
port=srpc.pf_getstr(SMPROFILE,'setup.port','8800')
return (addr,port)

#Connect to sms
def connectSMServer():
addr=getSMServer()
cli=srpc.SRPCClient()
if cli.Connect(addr[0],str(addr[1]))==0:
cli.Close()
print "Can't connect to sms server(%s,%s) !\r\n" % addr
return None
return cli


#Batch SM fee tool
def BatchGivenFee():

global logfile, logfp
print PRODUCT_INFO

#请输入文件名
file=raw_input( 'Input file name:')
print 'The INPUT FILENAME is %s\n' % file

file=string.strip(file)

#打开文件
if not os.path.exists( file) or not os.path.isfile( file):
print '%s is directory or does not exist.\r\n' % file
return

try:
fd=open(file,'r')
except IOError, err:
print err
print
return

head,tail=os.path.splitext(file)
if tail=='err':
logfile=head+'.err1'
else:
logfile=head+'.err'

#连接短信服务器
cli=connectSMServer()
if not isinstance(cli,srpc.SRPCClient):
fd.close()
return

#读取文件数据
filedata=''
while(1):
data=fd.read()
if not data: break
filedata+=data
fd.close()

records=filedata.split('\n')

smlib.SetValue(cli,smlib.SMPI_FEE_TYPE,'2')
smlib.SetValue(cli,smlib.SMPI_FEE_VALUE,'0')
smlib.SetValue(cli,smlib.SMPI_MT_FLAG,'3')
smlib.SetValue(cli,smlib.SMPI_REPORT_FLAG,'3')

ok=0
false=0
all=0
for r in records:
#对于每条计费记录
#{
if r.strip()=='END|': break


userfee=r.split('\t')
userfee=map(string.strip, userfee)

if len(userfee)<2: continue

mobile=userfee[0]
value=userfee[1]

try:
value=long(float(value)*100)
except:
value=0

if value==0: continue

smlib.SetValue(cli,smlib.SMPI_GIVEN_VALUE,str(value))
res=smlib.SendSingle(cli, mobile, '感谢您使用联通的如意邮箱!')

all+=1
if res[0]==1:
ok+=1
print '[%6d] [%s] ---- [%4s]: ok' % (all,mobile,value)
else:
false+=1
log(r)
print '[%6d] [%s] ---- [%4s]: %s,%s' % (all,mobile,value, res[0],res[1])

if all%5==0:
time.sleep(2)
#}

try:
if logfp:
logfp.close()
cli.Close()
except:
pass

print '%d mobile user processed.' % all
print '%d given feeing short messages have been sent to mobile user.' % ok

if false>0:
print '%d mobile user process failed, error has been writen to %s.' %( false, logfile)

return


if __name__=='__main__':
BatchGivenFee()
print
print 'Press any key to exit...'

raw_input()

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧