|
2 | 2 | from lxml import html
|
3 | 3 |
|
4 | 4 | def send(phone, password, receiver, message):
|
5 |
| - headers = { |
6 |
| - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
7 |
| - 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/70.0.3538.77 Chrome/70.0.3538.77 Safari/537.36', |
8 |
| - 'X-Requested-With': 'XMLHttpRequest', |
9 |
| - } |
10 |
| - |
11 |
| - response = requests.post('http://www.way2sms.com/re-login', |
12 |
| - data=dict( |
13 |
| - mobileNo=phone, |
14 |
| - password=password, |
15 |
| - CatType='', |
16 |
| - ), |
17 |
| - headers=headers, |
18 |
| - ) |
19 |
| - |
20 |
| - cookies = response.cookies.get_dict() |
21 |
| - |
22 |
| - headers['Cookie'] = '; '.join([f'{key}={value}' for key, value in cookies.items()]) |
23 |
| - |
24 |
| - response = requests.get('http://www.way2sms.com/send-sms', headers=headers) |
25 |
| - |
26 |
| - selector = html.fromstring(response.content) |
27 |
| - token = selector.xpath('//input[@id="Token"]/@value') |
28 |
| - token = token[0] if token else '' |
29 |
| - |
30 |
| - if not token: |
31 |
| - return send(phone, password, receiver, message) |
32 |
| - |
33 |
| - response = requests.post('http://www.way2sms.com/smstoss', headers=headers, data=dict( |
34 |
| - Token=token, |
35 |
| - message=message, |
36 |
| - toMobile=receiver, |
37 |
| - ssaction='undefined', |
38 |
| - )) |
39 |
| - |
40 |
| - return response.content |
| 5 | + with requests.Session() as s: |
| 6 | + headers = { |
| 7 | + 'Connection': 'keep-alive', |
| 8 | + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
| 9 | + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/70.0.3538.77 Chrome/70.0.3538.77 Safari/537.36', |
| 10 | + 'X-Requested-With': 'XMLHttpRequest', |
| 11 | + 'Origin': 'https://www.way2sms.com', |
| 12 | + 'Referer': 'https://www.way2sms.com/' |
| 13 | + } |
| 14 | + |
| 15 | + response = s.post('http://www.way2sms.com/re-login', |
| 16 | + data=dict( |
| 17 | + mobileNo=phone, |
| 18 | + password=password, |
| 19 | + CatType='', |
| 20 | + redirectPage='', |
| 21 | + pid='' |
| 22 | + ), |
| 23 | + headers=headers, |
| 24 | + ) |
| 25 | + |
| 26 | + response = s.get('http://www.way2sms.com/send-sms', headers=headers) |
| 27 | + |
| 28 | + selector = html.fromstring(response.content) |
| 29 | + token = selector.xpath('//input[@id="Token"]/@value') |
| 30 | + |
| 31 | + if not token: |
| 32 | + return send(phone, password, receiver, message) |
| 33 | + |
| 34 | + response = s.post('http://www.way2sms.com/smstoss', headers=headers, data=dict( |
| 35 | + Token=token[0], |
| 36 | + message=message, |
| 37 | + toMobile=receiver, |
| 38 | + ssaction='undefined', |
| 39 | + senderId='WAYSMS' |
| 40 | + )) |
| 41 | + |
| 42 | + return response.content |
0 commit comments