def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, app=None): self.app = None self.blocking = True self.stack = PyStack() if family not in (AF_INET, AF_INET6, AF_UNIX): raise error("Address family not supported by protocol " + family) else: self.family = family if type not in (SOCK_DGRAM, SOCK_STREAM): #SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET): raise error("Invalid argument " + type) else: self.type = type if app: self.app = app else: if type == SOCK_STREAM: self.app = _TCPSocket() elif type == SOCK_DGRAM: self.app = _UDPSocket() self.proto = proto if not app: if type == SOCK_STREAM: self.stack.register_tcp_application(self.app) elif type == SOCK_DGRAM: self.stack.register_udp_application(self.app) self.stack.run(doreactor=False)
def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, app=None): self.app = None self.blocking = True self.stack = PyStack() if family not in (AF_INET, AF_INET6, AF_UNIX): raise error("Address family not supported by protocol "+family) else: self.family = family if type not in (SOCK_DGRAM, SOCK_STREAM):#SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET): raise error("Invalid argument "+type) else: self.type = type if app: self.app = app else: if type == SOCK_STREAM: self.app = _TCPSocket() elif type == SOCK_DGRAM: self.app = _UDPSocket() self.proto = proto if not app: if type == SOCK_STREAM: self.stack.register_tcp_application(self.app) elif type == SOCK_DGRAM: self.stack.register_udp_application(self.app) self.stack.run(doreactor=False)
class socket: """ socket class that will replace the genuine one located in _socket """ def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, app=None): self.app = None self.blocking = True self.stack = PyStack() if family not in (AF_INET, AF_INET6, AF_UNIX): raise error("Address family not supported by protocol " + family) else: self.family = family if type not in (SOCK_DGRAM, SOCK_STREAM): #SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET): raise error("Invalid argument " + type) else: self.type = type if app: self.app = app else: if type == SOCK_STREAM: self.app = _TCPSocket() elif type == SOCK_DGRAM: self.app = _UDPSocket() self.proto = proto if not app: if type == SOCK_STREAM: self.stack.register_tcp_application(self.app) elif type == SOCK_DGRAM: self.stack.register_udp_application(self.app) self.stack.run(doreactor=False) def accept(self): """accept a connect in case of TCP Socket, raise an error for UDP""" if self.type == SOCK_DGRAM: raise error("[Errno 95] Operation not supported") else: app = self.app.accept() news = socket(AF_INET, SOCK_STREAM, 0, app, self.blocking) return news, app.get_conn_addr() def bind(self, (host, port)): """Bind the application on the given port regardless of if this is an UDP or TCP socket""" if self.type == SOCK_DGRAM: if not self.stack.is_udp_port_free(port): raise error("[Errno 98] Address already in use") elif self.type == SOCK_STREAM: if not self.stack.is_tcp_port_free(port): raise error("[Errno 98] Address already in use") self.app.bind( port, self.app, True) #send ip when the stack will listen on all interfaces
class socket: """ socket class that will replace the genuine one located in _socket """ def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, app=None): self.app = None self.blocking = True self.stack = PyStack() if family not in (AF_INET, AF_INET6, AF_UNIX): raise error("Address family not supported by protocol "+family) else: self.family = family if type not in (SOCK_DGRAM, SOCK_STREAM):#SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET): raise error("Invalid argument "+type) else: self.type = type if app: self.app = app else: if type == SOCK_STREAM: self.app = _TCPSocket() elif type == SOCK_DGRAM: self.app = _UDPSocket() self.proto = proto if not app: if type == SOCK_STREAM: self.stack.register_tcp_application(self.app) elif type == SOCK_DGRAM: self.stack.register_udp_application(self.app) self.stack.run(doreactor=False) def accept(self): """accept a connect in case of TCP Socket, raise an error for UDP""" if self.type == SOCK_DGRAM: raise error("[Errno 95] Operation not supported") else: app = self.app.accept() news = socket(AF_INET, SOCK_STREAM, 0, app, self.blocking) return news, app.get_conn_addr() def bind(self, (host, port)): """Bind the application on the given port regardless of if this is an UDP or TCP socket""" if self.type == SOCK_DGRAM: if not self.stack.is_udp_port_free(port): raise error("[Errno 98] Address already in use") elif self.type == SOCK_STREAM: if not self.stack.is_tcp_port_free(port): raise error("[Errno 98] Address already in use") self.app.bind(port, self.app, True) #send ip when the stack will listen on all interfaces