def create_connection(sock: socket, port: int, host: str) -> socket: sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # solution for: "socket.error: [Errno 98] Address already in use" sock.bind((host, port)) sock.listen(5) connection, client_address = sock.accept() # accept connection returns tuple print("Got connection from ", client_address) return connection
def bind_socket(my_socket: socket, *, bindAddress ="", bindPort = 0) -> bool: """""" #print(f"Binding: {bindPort}") try: ### TODO: Acquire local-IP. Will be 172.x in docker... print(f"Trying to bind socket({my_socket.fileno()}) with: [{bindAddress}] and Port: [{bindPort}]") ### TQ-TODO: IpV6 will expect a 4-tuple. (host, port, flowinfo, scopeid) my_socket.bind((bindAddress, bindPort)) except socket.error as err: print("Socket bind failed with error %s" % (err)) return False print("Socket bound successfully: ", my_socket.getsockname()) return True
def do_socket_logic(proxy_socket: socket, proxy_port_number): """ Example function for some helper logic, in case you want to be tidy and avoid stuffing the main function. Feel free to delete this function. """ proxy_socket.bind(("127.0.0.1", int(proxy_port_number))) proxy_socket.listen(10) cache = {} while True: client_socket, address = proxy_socket.accept() print(f"Started conn with {address}") start_new_thread(handle_client, (client_socket, cache, address)) proxy_socket.close() pass