{"id":6884,"library":"socketswap","title":"SocketSwap","description":"SocketSwap is a Python package designed to proxy traffic from any third-party libraries through a local TCP SOCKS5 Proxy. It is especially useful for applications and libraries that do not offer native proxying capabilities. The current version, 0.1.11, was released on September 29, 2023, and the project is generally considered stable.","status":"active","version":"0.1.11","language":"en","source_language":"en","source_url":"https://github.com/fyx99/SocketSwap","tags":["network","proxy","tcp","socks5","traffic-redirection","sockets","networking-tools"],"install":[{"cmd":"pip install socketswap","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ProxySwapContext","correct":"from SocketSwap import ProxySwapContext"}],"quickstart":{"code":"import socket\nimport logging\nimport threading\nimport time\nfrom SocketSwap import ProxySwapContext\n\n# Configure SocketSwap logging for visibility\nsocket_swap_logger = logging.getLogger(\"SocketSwap\")\nsocket_swap_logger.addHandler(logging.StreamHandler())\nsocket_swap_logger.setLevel(logging.DEBUG)\n\n# --- Dummy Remote Server (what ProxySwap will eventually connect to) ---\ndef dummy_remote_server(host, port):\n    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n    server_socket.bind((host, port))\n    server_socket.listen(1)\n    print(f\"Dummy Remote Server listening on {host}:{port}\")\n    try:\n        conn, addr = server_socket.accept()\n        with conn:\n            print(f\"Dummy Remote Server: Connected by {addr}\")\n            data = conn.recv(1024)\n            if data:\n                print(f\"Dummy Remote Server: Received {data.decode()}\")\n                conn.sendall(b\"Hello from Remote Server\")\n    except Exception as e:\n        print(f\"Dummy Remote Server error: {e}\")\n    finally:\n        server_socket.close()\n\n# --- Socket Factory for ProxySwapContext ---\ndef create_remote_socket(target_host, target_port):\n    remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n    remote_socket.connect((target_host, target_port))\n    return remote_socket\n\n# --- Main application logic using ProxySwapContext ---\ndef main():\n    REMOTE_HOST = \"127.0.0.1\"\n    REMOTE_PORT = 54321 # Port for the dummy remote server\n    LOCAL_PROXY_HOST = \"127.0.0.1\"\n    LOCAL_PROXY_PORT = 22222 # Port for the local proxy\n\n    # Start the dummy remote server in a separate thread\n    server_thread = threading.Thread(target=dummy_remote_server, args=(REMOTE_HOST, REMOTE_PORT))\n    server_thread.daemon = True # Allow main program to exit even if server thread is running\n    server_thread.start()\n    time.sleep(0.5) # Give server a moment to start\n\n    print(f\"Starting ProxySwapContext on {LOCAL_PROXY_HOST}:{LOCAL_PROXY_PORT}\")\n    try:\n        with ProxySwapContext(\n            socket_factory=create_remote_socket,\n            socket_factory_args=(REMOTE_HOST, REMOTE_PORT),\n            local_proxy_host=LOCAL_PROXY_HOST,\n            local_proxy_port=LOCAL_PROXY_PORT\n        ):\n            print(\"ProxySwapContext is active. Connecting client to local proxy...\")\n            # --- Dummy Client connecting to the local proxy ---\n            client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            client_socket.connect((LOCAL_PROXY_HOST, LOCAL_PROXY_PORT))\n            client_socket.sendall(b\"Hello via ProxySwap\")\n            response = client_socket.recv(1024)\n            print(f\"Client received from local proxy (and ultimately remote server): {response.decode()}\")\n            client_socket.close()\n            print(\"Client activity complete.\")\n\n        print(\"ProxySwapContext exited.\")\n    except Exception as e:\n        print(f\"An error occurred during ProxySwap operation: {e}\")\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"This quickstart demonstrates how to initialize and use `ProxySwapContext`. It sets up a local TCP proxy that redirects traffic to a 'dummy remote server'. A client then connects to the local proxy, and its traffic is routed to the remote server via SocketSwap. Logging is also configured for better visibility into the proxy's operations."},"warnings":[{"fix":"Ensure all four arguments are provided to `ProxySwapContext`, with `socket_factory_args` being an iterable (e.g., a tuple or list) even if empty.","message":"The `ProxySwapContext` constructor requires exactly four mandatory arguments: `socket_factory`, `socket_factory_args`, `local_proxy_host`, and `local_proxy_port`. Omitting any of these or providing incorrect types will lead to errors.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Add `import logging; socket_swap_logger = logging.getLogger(\"SocketSwap\"); socket_swap_logger.addHandler(logging.StreamHandler()); socket_swap_logger.setLevel(logging.DEBUG)` at the start of your application.","message":"Logging for SocketSwap is not enabled by default. To see debug information and operational details, you must manually configure the 'SocketSwap' logger with a handler and a logging level (e.g., `logging.DEBUG`).","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"Evaluate if the target library truly lacks proxy support before integrating SocketSwap. For libraries with native proxy settings, use their dedicated configurations first.","message":"SocketSwap is designed for libraries that *do not natively support proxying*. Using it with libraries that already have built-in proxy configurations (e.g., `requests`) might lead to unexpected behavior or conflicts, or simply be redundant.","severity":"gotcha","affected_versions":"0.1.0+"},{"fix":"For server-side sockets in your `socket_factory` or `local_proxy_host`, consider setting `socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)` to allow the reuse of an address that is in a `TIME_WAIT` state. Ensure proper closing of sockets when they are no longer needed.","message":"As SocketSwap operates on low-level sockets, misconfigurations or rapid restarts of proxied services can lead to 'Address already in use' errors. This typically occurs if a previous socket instance hasn't fully released the port.","severity":"gotcha","affected_versions":"0.1.0+"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}