CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day14/chatclient.py
Views: 729
1
from socket import socket
2
from threading import Thread
3
4
5
def main():
6
7
class RefreshScreenThread(Thread):
8
9
def __init__(self, client):
10
super().__init__()
11
self._client = client
12
13
def run(self):
14
while running:
15
data = self._client.recv(1024)
16
print(data.decode('utf-8'))
17
18
nickname = input('请输入你的昵称: ')
19
myclient = socket()
20
myclient.connect(('10.7.189.118', 12345))
21
running = True
22
RefreshScreenThread(myclient).start()
23
while running:
24
content = input('请发言: ')
25
if content == 'byebye':
26
myclient.send(content.encode('utf-8'))
27
running = False
28
else:
29
msg = nickname + ': ' + content
30
myclient.send(msg.encode('utf-8'))
31
32
33
if __name__ == '__main__':
34
main()
35
36