socket.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import _socket
  2. AF_INET = 2
  3. SOCK_STREAM = 1
  4. class socket(_socket.socket):
  5. family = AF_INET
  6. type = SOCK_STREAM
  7. sockfd = 0
  8. client_sockfd = 0
  9. client_addr = ''
  10. protocol = 0
  11. def __init__(self, *vars):
  12. if len(vars) > 0:
  13. self.family = vars[0]
  14. if len(vars) > 1:
  15. self.type = vars[1]
  16. if len(vars) > 2:
  17. self.proto = vars[2]
  18. self._init()
  19. def bind(self, host_port):
  20. host = host_port[0]
  21. port = host_port[1]
  22. return self._bind(host, port)
  23. def listen(self, num):
  24. return self._listen(num)
  25. def accept(self):
  26. self._accept()
  27. client = socket()
  28. client.sockfd = self.client_sockfd
  29. return (client, self.client_addr)
  30. def send(self, data):
  31. return self._send(data)
  32. def close(self):
  33. self._close()
  34. def connect(self, host_port):
  35. host = host_port[0]
  36. port = host_port[1]
  37. return self._connect(host, port)
  38. def recv(self, num):
  39. return self._recv(num)
  40. def gethostname():
  41. return _socket._gethostname()