Skip to content

Commit f5d8cea

Browse files
committed
add files
0 parents  commit f5d8cea

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.mdb
2+
*.ldb
3+
*.sln
4+
*.config
5+
Debug/
6+
Release/
7+
obj/
8+
config.inc.php
9+
.idea/*
10+
*.pyc

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## pyfetion
2+
3+
API refernce:
4+
5+
https://code.google.com/p/php-fetion/source/browse/trunk/lib/PHPFetion.php
6+
7+
### How to use
8+
9+
```
10+
yourmob = "136XXXXXXXX"
11+
password = '123456'
12+
13+
send_to = '138XXXXXXXX'
14+
msg = 'SMS just for test!'
15+
16+
m = Fetion(yourmob, password)
17+
18+
# DEBUG
19+
20+
m = Fetion(yourmob, password, 1)
21+
22+
try:
23+
print m.send_msg(send_to, msg)
24+
except FetionError, e:
25+
print e
26+
```

pyfetion.py

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
5+
"""
6+
@version: v0.1
7+
@author: phpergao
8+
@license: Apache Licence
9+
10+
@site: http://www.phpgao.com
11+
@software: PyCharm
12+
@file: fetion.py
13+
@time: 15-1-27 下午8:55
14+
python调用飞信发送短信
15+
"""
16+
17+
import urllib2
18+
import urllib
19+
import cookielib
20+
import re
21+
22+
23+
class FetionError(Exception):
24+
def __init__(self, code=0):
25+
self.code = code
26+
27+
def __str__(self):
28+
errors = {
29+
0: 'Unknown error',
30+
404: 'Wrong mobile number',
31+
301: 'Login error',
32+
403: 'Get token error',
33+
400: 'Error happens while requesting',
34+
401: 'Error happens while sending to myself',
35+
402: 'Error happens while sending to others'
36+
}
37+
return errors[self.code]
38+
39+
40+
class Fetion():
41+
def __init__(self, mob, password, debug=False):
42+
self.base_url = 'http://f.10086.cn'
43+
self.mob = mob
44+
self.password = password
45+
self.cookie = ''
46+
self.debug = debug
47+
self.friends = self.get_friends_list()
48+
self.do_login()
49+
50+
def __getattr__(self, attr):
51+
urls = {
52+
'login_url': '/huc/user/space/login.do',
53+
'check_login_url': '/im/login/cklogin.action',
54+
'send_to_me_url': '/im/user/sendMsgToMyselfs.action',
55+
'send_to_other_url': '/im/chat/sendMsg.action?touserid=',
56+
'get_uid_url': '/im/index/searchOtherInfoList.action',
57+
'csrftoken_url': '/im/chat/toinputMsg.action?touserid=',
58+
}
59+
return urls[attr]
60+
61+
def do_login(self):
62+
post_data = {
63+
'mobilenum': self.mob,
64+
'password': self.password,
65+
"m": "submit",
66+
"fr": "space",
67+
"backurl": "http://f.10086.cn/"
68+
}
69+
self.send(self.login_url, post_data)
70+
self.do_check_login()
71+
72+
def do_check_login(self):
73+
self.send(self.check_login_url)
74+
75+
def send_msg(self, mob, msg):
76+
if mob == self.mob:
77+
return self.send_to_myself(msg)
78+
else:
79+
return self.send_to_other(mob, msg)
80+
81+
def send_to_myself(self, msg):
82+
msg = {'msg': msg}
83+
xml = self.send(self.send_to_me_url, msg)
84+
if re.search(r'(短信发送成功)', xml) is None:
85+
raise FetionError(401)
86+
return True
87+
88+
def send_to_other(self, mob, msg):
89+
uid = self.get_uid(mob)
90+
csrf_token = self.getcsrftoken(uid)
91+
uri = self.send_to_other_url + uid
92+
msg = {'msg': msg, 'csrfToken': csrf_token}
93+
xml = self.send(uri, msg)
94+
if re.search(r'(发送消息成功)', xml) is None:
95+
raise FetionError(402)
96+
return True
97+
98+
def get_uid(self, mob):
99+
data = {'searchText': mob}
100+
xml = self.send(self.get_uid_url, data)
101+
match = re.search(r'toinputMsg\.action\?touserid=(?P<uid>\d+)', xml)
102+
if match:
103+
return match.group('uid')
104+
else:
105+
raise FetionError(404)
106+
107+
def getcsrftoken(self, uid):
108+
if uid in self.friends:
109+
return self.friends[uid]
110+
uri = self.csrftoken_url + uid
111+
xml = self.send(uri)
112+
113+
match = re.search(r'name="csrfToken"\svalue="(?P<token>\w+)"', xml, re.M)
114+
if match:
115+
token = match.group('token')
116+
self.save_token(uid, token)
117+
return token
118+
else:
119+
raise FetionError(403)
120+
121+
def save_token(self, uid, token):
122+
# somewhere to save token
123+
self.friends[uid] = token
124+
125+
def get_friends_list(self):
126+
# somewhere to get token
127+
return {}
128+
129+
def send(self, uri, data=''):
130+
url = self.base_url + str(uri)
131+
req = urllib2.Request(url)
132+
# cookie enabled
133+
if self.cookie == '':
134+
self.cookie = cookielib.CookieJar()
135+
cookie_handler = urllib2.HTTPCookieProcessor(self.cookie)
136+
else:
137+
cookie_handler = urllib2.HTTPCookieProcessor(self.cookie)
138+
139+
if self.debug:
140+
http_handler = urllib2.HTTPHandler(debuglevel=1)
141+
opener = urllib2.build_opener(cookie_handler, http_handler)
142+
else:
143+
opener = urllib2.build_opener(cookie_handler)
144+
145+
req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0')
146+
req.add_header('Content-Type', 'application/x-www-form-urlencoded')
147+
req.add_header('Cache-Control', 'no-cache')
148+
req.add_header('Accept', '*/*')
149+
req.add_header('Connection', 'close')
150+
# post data
151+
if data:
152+
post_data = urllib.urlencode(data)
153+
req.add_data(post_data)
154+
req.add_header('Content-Length', len(post_data))
155+
try:
156+
response = opener.open(req)
157+
except urllib2.URLError, error:
158+
raise FetionError(400)
159+
exit()
160+
return response.read()
161+
162+
163+
if __name__ == '__main__':
164+
165+
yourmob = "136XXXXXXXX"
166+
password = '123456'
167+
168+
send_to = '138XXXXXXXX'
169+
msg = '测试短信,请无视'
170+
171+
m = Fetion(yourmob, password)
172+
try:
173+
print m.send_msg(send_to, msg)
174+
except FetionError, e:
175+
print e

0 commit comments

Comments
 (0)