-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
64 lines (49 loc) · 1.34 KB
/
client.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
import socket
import urllib.request
import os
import time
import datetime
import json
import threading
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
sendMsg("{\"type\":\"init\"}")
self.helloLabel = Label(self, text='Hello, world!')
self.helloLabel.pack()
self.quitButton = Button(self, text='Quit', command=self.quit)
self.quitButton.pack()
app=None
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# # 建立连接:
s.connect(("127.0.0.1", 80))
def sendMsg(msg):
s.send(msg.encode(encoding='utf_8'))
def msgAction(msg):
jsonData=json.loads(msg)
app.helloLabel.config(text=msg)
def getMsg():
while True:
# 每次最多接收1k字节:
d = s.recv(1024)
if d:
msgAction(d.decode("utf8"))
else:
break
def mainFrame():
global app
app = Application()
# 设置窗口标题:
app.master.title('Hello World')
# 主消息循环:
app.mainloop()
t=threading.Thread(target=mainFrame)
t1=threading.Thread(target=getMsg)
t.start()
t1.start()
t.join()
t1.join()