fork download
  1. # your code goes hereimport socket
  2. import threading
  3.  
  4. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5.  
  6. sock.connect(('localhost', 5550))
  7. sock.send(b'1')
  8. print(sock.recv(1024).decode())
  9. #nickName = input('input your nickname: ')
  10. #sock.send(nickName.encode())
  11.  
  12. def sendThreadFunc():
  13. #while True:
  14. try:
  15. myword = input()
  16. sock.send(myword.encode())
  17. #print(sock.recv(1024).decode())
  18. except ConnectionAbortedError:
  19. print('Server closed this connection!')
  20. except ConnectionResetError:
  21. print('Server is closed!')
  22.  
  23. def recvThreadFunc():
  24. #while True:
  25. try:
  26. otherword = sock.recv(1024)
  27. if otherword == 'quit':
  28. sock.close()
  29. elif otherword:
  30. print(otherword.decode())
  31. else:
  32. pass
  33. except ConnectionAbortedError:
  34. print('Server closed this connection!')
  35.  
  36. except ConnectionResetError:
  37. print('Server is closed!')
  38.  
  39.  
  40. th1 = threading.Thread(target=sendThreadFunc)
  41. th2 = threading.Thread(target=recvThreadFunc)
  42. threads = [th1, th2]
  43.  
  44. for t in threads :
  45. t.setDaemon(True)
  46. t.start()
  47. t.join()
Runtime error #stdin #stdout #stderr 0.02s 28048KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
NameError: name 'socket' is not defined