text_socket.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import socket
  2. from supervisor.compat import PY3, as_string, as_bytes
  3. bin_socket = socket.socket
  4. if PY3:
  5. class text_socket(bin_socket):
  6. def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM,
  7. proto=0, fileno=None):
  8. bin_socket.__init__(self, family, type, proto, fileno)
  9. def recv(self, *args, **kwargs):
  10. return as_string(bin_socket.recv(self, *args, **kwargs))
  11. def recvfrom(self, *args, **kwargs):
  12. reply, whence = bin_socket.recvfrom(self, *args, **kwargs)
  13. reply = as_string(reply)
  14. return reply, whence
  15. def send(self, data, *args, **kwargs):
  16. b = as_bytes(data)
  17. return bin_socket.send(self, b, *args, **kwargs)
  18. def sendall(self, data, *args, **kwargs):
  19. return bin_socket.sendall(self, as_bytes(data), *args, **kwargs)
  20. def sendto(self, data, *args, **kwargs):
  21. return bin_socket.sendto(self, as_bytes(data), *args, **kwargs)
  22. def accept(self):
  23. # sock, addr = bin_socket.accept(self)
  24. # sock = text_socket(self.family, self.type, self.proto, fileno=sock.fileno())
  25. fd, addr = self._accept()
  26. sock = text_socket(self.family, self.type, self.proto, fileno=fd)
  27. # Issue #7995: if no default timeout is set and the listening
  28. # socket had a (non-zero) timeout, force the new socket in blocking
  29. # mode to override platform-specific socket flags inheritance.
  30. if socket.getdefaulttimeout() is None and self.gettimeout():
  31. sock.setblocking(True)
  32. return sock, addr
  33. text_socket.__init__.__doc__ = bin_socket.__init__.__doc__
  34. else:
  35. text_socket = bin_socket