socket.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if type(host) != str:
  38. print('Error: host must be a string')
  39. raise
  40. if type(port) != int:
  41. print('Error: port must be an integer')
  42. raise
  43. return self._connect(host, port)
  44. def recv(self, num):
  45. return self._recv(num)
  46. def setblocking(self, sta):
  47. return self._setblocking(sta)
  48. def gethostname():
  49. return _socket._gethostname()
  50. def gethostbyname(host):
  51. return _socket._gethostbyname(host)