数据库和抢红包的交互

数据库和抢红包的交互源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pymysql
import re
import requests
import json
import time
host=''
port=3306
user=''
password=''
db=''
tabel=''
def get_message():
connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
#print('ok')
sql="SELECT * FROM `%s`"%(tabel)
cursor.execute(sql)
result= cursor.fetchall()
#print(result)
if result:
return result
else:
return False
def add_mysql(r_id,r_money,r_type):
connection = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()

sql1="UPDATE `%s` SET `r_money` = '%s' WHERE `%s`.`r_id` = %s;"%(tabel,str(r_money),tabel,str(r_id))
sql2="UPDATE `%s` SET `r_type` = '%s' WHERE `%s`.`r_id` = %s;"%(tabel,str(r_type),tabel,str(r_id))
try:
cursor.execute(sql1)
cursor.execute(sql2)
connection.commit()
except:
connection.rollback()
connection.close()
return False
connection.close()
return True
def post_web(phone,url):
data={
'phone': phone,
'url': url
}
headers={
'user-agent': 'Mozilla/5.0 (Linux; Android 5.1; m1 metal Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043409 Safari/537.36 V1ANDSQ7.2.5744YYBD QQ/7.2.5.3305 NetType/WIFI WebP/0.3.0 Pixel/1080',
'x-user-token':''
}
web='https://mtdhb.z.xxooweb.com/user/receiving'
html=requests.post(web,headers=headers,data=data)
text = json.loads(html.text)
#print(text)
if text['code']==0:
return text
else:
return False;
def get_web():
headers={
'user-agent': 'Mozilla/5.0 (Linux; Android 5.1; m1 metal Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043409 Safari/537.36 V1ANDSQ7.2.5744YYBD QQ/7.2.5.3305 NetType/WIFI WebP/0.3.0 Pixel/1080',
'x-user-token':''
}
web=r'https://mtdhb.z.xxooweb.com/user/receiving'
html=requests.get(web,headers=headers)
text = json.loads(html.text)
#print(text['data'])
return text['data'][0]['price']
def note(r_phone,r_money):
file=open("note.txt","a")
file.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+' '+str(r_phone)+'----'+str(r_money)+'\n')
file.close()


if __name__ == "__main__" :
while(1):
print("starting……")
try:
text=get_message()
for message in text:
r_id=message['r_id']
#print(message['r_type'] )
if(not message['r_type'] or message['r_type']=='0' or message['r_type']==0):
is_true=post_web(message['r_tel'],message['r_url'])
print(message['r_tel'],str(is_true))
if(is_true):
time.sleep(10)
now=get_web()
print(now)
if(now):
add_mysql(r_id,now,1)
print("导入一条已抢数据")
note(message['r_tel'],now)
else:
add_mysql(r_id,0,2)
print("导入一条被抢数据")
else:
add_mysql(r_id,0,2)
print("导入一条被抢数据")
except :
print("error")
continue
time.sleep(5)