chatclient.py 847 B

12345678910111213141516171819202122232425262728293031323334
  1. from socket import socket
  2. from threading import Thread
  3. def main():
  4. class RefreshScreenThread(Thread):
  5. def __init__(self, client):
  6. super().__init__()
  7. self._client = client
  8. def run(self):
  9. while running:
  10. data = self._client.recv(1024)
  11. print(data.decode('utf-8'))
  12. nickname = input('请输入你的昵称: ')
  13. myclient = socket()
  14. myclient.connect(('10.7.189.118', 12345))
  15. running = True
  16. RefreshScreenThread(myclient).start()
  17. while running:
  18. content = input('请发言: ')
  19. if content == 'byebye':
  20. myclient.send(content.encode('utf-8'))
  21. running = False
  22. else:
  23. msg = nickname + ': ' + content
  24. myclient.send(msg.encode('utf-8'))
  25. if __name__ == '__main__':
  26. main()