CyberSocket.py 738 B

12345678910111213141516171819202122232425262728293031
  1. import socketserver
  2. import socket
  3. class CyberSocketServer(socketserver.BaseRequestHandler):
  4. def handle(self):
  5. print("connection from",self.client_address)
  6. while True:
  7. data = self.request.recv(2048)
  8. if not data:
  9. break
  10. print('recv:', data)
  11. class CyberSocket:
  12. def __init__(self):
  13. pass
  14. def start(self):
  15. hostname= socket.gethostname()#获取本地主机名
  16. sysinfo = socket.gethostbyname_ex(hostname)
  17. hostip=sysinfo[2][2]
  18. self.server = socketserver.ThreadingTCPServer((hostip, 2233), CyberSocketServer)
  19. print("server created")
  20. self.server.serve_forever()
  21. def send_data(self,data):
  22. request, client_address = self.server.get_request()
  23. data = data+"\n"
  24. print(data)
  25. request.sendall(data.encode())