-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.py
76 lines (68 loc) · 1.93 KB
/
transaction.py
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
import urllib.request
import os
import time
import datetime
import json
import socket
def msgAction(msg):
jsonData=json.loads(msg)
print(jsonData["type"])
getPlayerInfo()
global stockValue
for index in range(len(player["position"])):
stockValue+=float(stockPrice[index])*float(player["position"][index]["number"])
return str(player["money"]+stockValue).encode("utf8")
def getAllPrice(codeList):
#暂时用单线程实现
data=selectInfo(codeList)
msgs=data.split(';')
for index,msg in enumerate(msgs):
if msg.strip()=="":
continue
msgAll=msg[msg.find("\"")+1:msg.rfind("\"")]
msgAll=msgAll.split("~")
global stockPrice
stockPrice.append(msgAll[3])
def selectInfo(codeList):
param="q="
for shares in codeList:
param+=shares["code"]+","
req = urllib.request.Request("http://qt.gtimg.cn/%s" % param,method="GET")
data=urllib.request.urlopen(req).read().decode('GBK')
return data
def getPlayerInfo():
file=open("C://admin.txt")
global player
player=json.loads(file.read())
print(player)
getAllPrice(player["position"])
def updatePlayerInfo(player):
file=open("C://admin.txt")
file.write(player)
file.close()
#交易开关
flag=False
#用户信息
player={}
#当前交易单列表
order=[]
stockPrice=[]
stockValue=0
# getPlayerInfo()
# for index in range(len(player["position"])):
# stockValue+=float(stockPrice[index])*float(player["position"][index]["number"])
# print(player["money"]+stockValue)
#socket监听
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 80))
s.listen(5)
sock, addr=s.accept()
while True:
data=sock.recv(1024)
sock.send(msgAction(data.decode("utf8")))
#开启交易
def start():
flag=True
#关闭交易
def close():
flag=False