Assignment|socket通信的python实现

代码

编写一个员工考勤打卡网络应用程序。要求使用可靠的、面向连接的网络通信完成客户端提交工号及打卡时间,服务器端将接收到的工号和打卡时间显示到屏幕上。(编程语言不限)。

服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from socket import *

serverPort = 12000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('192.168.64.1', serverPort))
serverSocket.listen(1)
print("The server is ready to receive")
while True:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024).decode()

while sentence:
print(sentence)
break

capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence.encode())
connectionSocket.close()

客户端

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
from socket import *
import datetime
from tkinter import *
from tkinter import ttk

def sendPack():
serverName = '192.168.64.1'
sentence = ent1.get()
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
# sentence = input('Input Your ID:')
now = datetime.datetime.now()
sentence = sentence + " " + str(now)
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024)
print('From Server: ', modifiedSentence.decode())
clientSocket.close()

root = Tk()
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="工号").grid(column=0, row=0)
ent1 = ttk.Entry(frm)
ent1.grid(column=1, row=0)
ttk.Button(frm, text="打卡", command=lambda: sendPack()).grid(column=2, row=0)
root.mainloop()