### Start the SOCKS proxy container Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/development.md Starts the Dante SOCKS server container in the background using Docker Compose. ```bash docker-compose -f docker/docker-compose.yml up -d ``` -------------------------------- ### Establish SOCKS4A Connection with Domain Names Source: https://context7.com/sethmlarson/socksio/llms.txt Illustrates using SOCKS4A to establish a connection to a domain name, allowing the proxy to perform DNS resolution. Includes examples using both string and tuple formats for addresses. ```python import socket from socksio import socks4 sock = socket.create_connection(("localhost", 1080)) conn = socks4.SOCKS4Connection(user_id=b"socksio") # SOCKS4A allows domain names - proxy will resolve DNS request = socks4.SOCKS4ARequest.from_address( socks4.SOCKS4Command.CONNECT, "example.com:80" # Domain name instead of IP ) # Alternative tuple format request = socks4.SOCKS4ARequest.from_address( socks4.SOCKS4Command.CONNECT, ("api.github.com", 443) ) conn.send(request) sock.sendall(conn.data_to_send()) response_data = sock.recv(1024) reply = conn.receive_data(response_data) if reply.reply_code == socks4.SOCKS4ReplyCode.REQUEST_GRANTED: print(f"Connected! Bound address: {reply.addr}:{reply.port}") # Now send application data through the tunnel sock.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") ``` -------------------------------- ### Create SOCKS4 Requests Source: https://context7.com/sethmlarson/socksio/llms.txt Shows different ways to create SOCKS4 requests, including using address tuples, string formats, and manual parameter specification. Also demonstrates packing requests into bytes and lists available commands. ```python from socksio import socks4 # Create request using tuple format (address, port) request = socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, ("192.168.1.100", 443), user_id=b"optional_user" ) # Create request using string format "address:port" request = socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, "192.168.1.100:443" ) # Manual creation with explicit parameters request = socks4.SOCKS4Request( command=socks4.SOCKS4Command.CONNECT, port=443, addr=b"\xc0\xa8\x01\x64", # 192.168.1.100 as bytes user_id=b"myuser" ) # Pack the request into bytes for sending packed_data = request.dumps(user_id=b"myuser") print(f"Packed request: {packed_data.hex()}") # Output: 0401bb01c0a80164myuser00 # Available commands print(socks4.SOCKS4Command.CONNECT) # Connect to a target host print(socks4.SOCKS4Command.BIND) # Bind a port for incoming connection ``` -------------------------------- ### Construct SOCKS5 command requests Source: https://context7.com/sethmlarson/socksio/llms.txt Demonstrates creating command requests for different address types (IPv4, IPv6, domain) and packing them for transmission. ```python from socksio import socks5 # Connect using domain name (proxy resolves DNS) request = socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, ("api.github.com", 443) ) # Connect using IPv4 address request = socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, "192.168.1.1:8080" ) # Connect using IPv6 address request = socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, ("2001:db8::1", 443) ) # Manual creation with explicit address type request = socks5.SOCKS5CommandRequest( command=socks5.SOCKS5Command.CONNECT, atype=socks5.SOCKS5AType.DOMAIN_NAME, addr=b"example.com", port=80 ) # Pack to bytes packed = request.dumps() print(f"Command request: {packed.hex()}") # Available commands print(socks5.SOCKS5Command.CONNECT) # 0x01 - Connect to target print(socks5.SOCKS5Command.BIND) # 0x02 - Bind port for incoming print(socks5.SOCKS5Command.UDP_ASSOCIATE) # 0x03 - UDP associate (not implemented) # Address types print(socks5.SOCKS5AType.IPV4_ADDRESS) # 0x01 print(socks5.SOCKS5AType.DOMAIN_NAME) # 0x03 print(socks5.SOCKS5AType.IPV6_ADDRESS) # 0x04 ``` -------------------------------- ### Configure SOCKS5 Authentication Methods Source: https://context7.com/sethmlarson/socksio/llms.txt Initiate SOCKS5 handshake by proposing supported authentication methods. ```python from socksio import socks5 # Request no authentication (public proxy) request = socks5.SOCKS5AuthMethodsRequest([ socks5.SOCKS5AuthMethod.NO_AUTH_REQUIRED ]) # Request multiple methods - server chooses one request = socks5.SOCKS5AuthMethodsRequest([ socks5.SOCKS5AuthMethod.NO_AUTH_REQUIRED, socks5.SOCKS5AuthMethod.USERNAME_PASSWORD, ]) # Pack the request to bytes packed = request.dumps() print(f"Auth request bytes: {packed.hex()}") # Output: 050200 02 (version=5, nmethods=2, methods=[NO_AUTH, USERNAME_PASSWORD]) # Available authentication methods print(socks5.SOCKS5AuthMethod.NO_AUTH_REQUIRED) # 0x00 - No authentication print(socks5.SOCKS5AuthMethod.GSSAPI) # 0x01 - GSSAPI (not implemented) print(socks5.SOCKS5AuthMethod.USERNAME_PASSWORD) # 0x02 - Username/password print(socks5.SOCKS5AuthMethod.NO_ACCEPTABLE_METHODS) # 0xFF - Server rejects all methods ``` -------------------------------- ### Create a socket connection Source: https://github.com/sethmlarson/socksio/blob/master/README.md Establishes a standard TCP connection to a local SOCKS proxy. ```python import socket sock = socket.create_connection(("localhost", 8080)) ``` -------------------------------- ### Establish SOCKS4 Connection and Send Data Source: https://context7.com/sethmlarson/socksio/llms.txt Demonstrates establishing a SOCKS4 connection, sending a CONNECT request, receiving a reply, and then sending application data through the established tunnel. Requires a running SOCKS4 proxy on localhost:1080. ```python import socket from socksio import socks4 # Create a TCP connection to the SOCKS4 proxy sock = socket.create_connection(("localhost", 1080)) # Initialize the SOCKS4 connection with a user ID conn = socks4.SOCKS4Connection(user_id=b"myapp") # Create a CONNECT request to an IP address (SOCKS4 only supports IPv4) request = socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, ("142.250.178.14", 80) # Can also use "142.250.178.14:80" ) # Queue the request and get data to send conn.send(request) data_to_send = conn.data_to_send() sock.sendall(data_to_send) # Receive and parse the proxy's reply response_data = sock.recv(1024) reply = conn.receive_data(response_data) # Check if the connection was successful if reply.reply_code == socks4.SOCKS4ReplyCode.REQUEST_GRANTED: # Tunnel is established - send data directly through the proxy sock.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") http_response = sock.recv(4096) print(http_response.decode()) else: print(f"Proxy connection failed: {reply.reply_code}") # reply_code can be: REQUEST_REJECTED_OR_FAILED, CONNECTION_FAILED, AUTHENTICATION_FAILED ``` -------------------------------- ### Handle SOCKSIO exceptions Source: https://context7.com/sethmlarson/socksio/llms.txt Demonstrates handling of SOCKSError for general library issues and ProtocolError for malformed responses or invalid state transitions. ```python from socksio import SOCKSError, ProtocolError, socks4, socks5 # SOCKSError - general library errors try: # SOCKS4 requires user_id - this will raise SOCKSError request = socks4.SOCKS4Request( command=socks4.SOCKS4Command.CONNECT, port=80, addr=b"\xc0\xa8\x01\x01" ) request.dumps() # No user_id provided except SOCKSError as e: print(f"SOCKS error: {e}") # "SOCKS4 requires a user_id, none was specified" # SOCKSError - invalid address type for protocol try: # SOCKS4 doesn't support domain names socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, ("example.com", 80) # Domain name not allowed ) except SOCKSError as e: print(f"Address error: {e}") # "IPv6 addresses and domain names are not supported by SOCKS4" # ProtocolError - malformed server responses try: socks4.SOCKS4Reply.loads(b"\x01\x5a") # Invalid reply format except ProtocolError as e: print(f"Protocol error: {e}") # "Malformed reply" # ProtocolError - wrong protocol state try: conn = socks5.SOCKS5Connection() # Trying to send command before authentication conn.send(socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, ("example.com", 80) )) except ProtocolError as e: print(f"State error: {e}") # "SOCKS5 connections must be authenticated before sending a request" ``` -------------------------------- ### Track SOCKS5 protocol state Source: https://context7.com/sethmlarson/socksio/llms.txt Initializes a SOCKS5Connection to monitor the protocol state machine transitions. ```python from socksio import socks5 conn = socks5.SOCKS5Connection() print(f"Initial state: {conn.state}") # CLIENT_AUTH_REQUIRED # State transitions through the protocol # CLIENT_AUTH_REQUIRED -> SERVER_AUTH_REPLY (after sending auth methods) # SERVER_AUTH_REPLY -> CLIENT_WAITING_FOR_USERNAME_PASSWORD (if auth required) # SERVER_AUTH_REPLY -> CLIENT_AUTHENTICATED (if no auth) # CLIENT_WAITING_FOR_USERNAME_PASSWORD -> SERVER_VERIFY_USERNAME_PASSWORD # SERVER_VERIFY_USERNAME_PASSWORD -> CLIENT_AUTHENTICATED (success) # SERVER_VERIFY_USERNAME_PASSWORD -> MUST_CLOSE (failure) # CLIENT_AUTHENTICATED -> TUNNEL_READY (after successful connect) ``` -------------------------------- ### Manage SOCKS5 Connection Source: https://context7.com/sethmlarson/socksio/llms.txt Full workflow for a SOCKS5 connection, including authentication and command execution. ```python import socket from socksio import socks5 sock = socket.create_connection(("localhost", 1080)) conn = socks5.SOCKS5Connection() # Step 1: Send authentication methods request auth_request = socks5.SOCKS5AuthMethodsRequest([ socks5.SOCKS5AuthMethod.NO_AUTH_REQUIRED, socks5.SOCKS5AuthMethod.USERNAME_PASSWORD, ]) conn.send(auth_request) sock.sendall(conn.data_to_send()) # Step 2: Receive authentication method reply auth_data = sock.recv(1024) auth_reply = conn.receive_data(auth_data) print(f"Server chose auth method: {auth_reply.method}") # Step 3: Handle username/password authentication if required if auth_reply.method == socks5.SOCKS5AuthMethod.USERNAME_PASSWORD: auth_request = socks5.SOCKS5UsernamePasswordRequest( username=b"myusername", password=b"mypassword" ) conn.send(auth_request) sock.sendall(conn.data_to_send()) auth_response = sock.recv(1024) auth_result = conn.receive_data(auth_response) if not auth_result.success: raise Exception("Authentication failed") print("Authenticated successfully") # Step 4: Send connect command command_request = socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, ("example.com", 443) # SOCKS5 supports domain names, IPv4, and IPv6 ) conn.send(command_request) sock.sendall(conn.data_to_send()) # Step 5: Receive command reply command_data = sock.recv(1024) command_reply = conn.receive_data(command_data) if command_reply.reply_code == socks5.SOCKS5ReplyCode.SUCCEEDED: print(f"Tunnel ready! Bound: {command_reply.addr}:{command_reply.port}") print(f"Connection state: {conn.state}") # SOCKS5State.TUNNEL_READY # Send application data sock.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n") ``` -------------------------------- ### Create Socket Connection to Proxy Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Establishes a basic TCP connection to a SOCKS proxy server. Ensure the proxy is running on the specified host and port. ```python import socket sock = socket.create_connection(("localhost", 8080)) ``` -------------------------------- ### Create a SOCKS5 tunnel Source: https://context7.com/sethmlarson/socksio/llms.txt Establishes a SOCKS5 connection with optional authentication and command request handling. ```python def create_socks5_tunnel(proxy_addr, target_addr, username=None, password=None): import socket sock = socket.create_connection(proxy_addr) conn = socks5.SOCKS5Connection() # Auth negotiation methods = [socks5.SOCKS5AuthMethod.NO_AUTH_REQUIRED] if username: methods.append(socks5.SOCKS5AuthMethod.USERNAME_PASSWORD) conn.send(socks5.SOCKS5AuthMethodsRequest(methods)) sock.sendall(conn.data_to_send()) auth_reply = conn.receive_data(sock.recv(1024)) # Handle authentication if auth_reply.method == socks5.SOCKS5AuthMethod.USERNAME_PASSWORD: conn.send(socks5.SOCKS5UsernamePasswordRequest(username, password)) sock.sendall(conn.data_to_send()) auth_result = conn.receive_data(sock.recv(1024)) if not auth_result.success: sock.close() raise Exception("Auth failed") # Connect conn.send(socks5.SOCKS5CommandRequest.from_address( socks5.SOCKS5Command.CONNECT, target_addr )) sock.sendall(conn.data_to_send()) reply = conn.receive_data(sock.recv(1024)) if reply.reply_code == socks5.SOCKS5ReplyCode.SUCCEEDED: assert conn.state == socks5.SOCKS5State.TUNNEL_READY return sock sock.close() raise Exception(f"Connect failed: {reply.reply_code}") ``` -------------------------------- ### Initialize SOCKS4 Connection Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Initializes a SOCKS4 connection object. The SOCKS4 protocol requires a user ID, which must be provided as bytes. ```python from socksio import socks4 # The SOCKS4 protocol requires a `user_id` to be supplied. conn = socks4.SOCKS4Connection(user_id=b"socksio") ``` -------------------------------- ### Create SOCKS5 Username/Password Request Source: https://context7.com/sethmlarson/socksio/llms.txt Encapsulate credentials for SOCKS5 authentication when the server requires it. ```python from socksio import socks5 # Create authentication request with credentials auth_request = socks5.SOCKS5UsernamePasswordRequest( username=b"proxyuser", password=b"secretpass123" ) # Pack to bytes for transmission packed = auth_request.dumps() print(f"Auth request: {packed.hex()}") # Format: version(1) + ulen(1) + username + plen(1) + password ``` -------------------------------- ### SOCKS5 Request/Reply Objects Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks5.md Classes for constructing SOCKS5 requests and parsing replies from the proxy server. ```APIDOC ## SOCKS5 Request and Reply Classes ### SOCKS5AuthMethodsRequest - **dumps()** -> bytes: Packs the authentication methods request into binary. ### SOCKS5AuthReply - **loads(data: bytes)** -> SOCKS5AuthReply: Unpacks authentication reply data. ### SOCKS5UsernamePasswordRequest - **dumps()** -> bytes: Packs the username/password authentication request. ### SOCKS5UsernamePasswordReply - **loads(data: bytes)** -> SOCKS5UsernamePasswordReply: Unpacks the username/password authentication reply. ### SOCKS5CommandRequest - **dumps()** -> bytes: Packs the command request. - **from_address(command, address)** -> SOCKS5CommandRequest: Convenience method to build a request from a host/port string or tuple. ### SOCKS5Reply - **loads(data: bytes)** -> SOCKS5Reply: Unpacks the command reply data. ``` -------------------------------- ### Create SOCKS4 Request Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Constructs a SOCKS4 request object for a specific command and destination address. SOCKS4 does not support domain names, so an IP address must be used. ```python # SOCKS4 does not allow domain names, below is an IP for google.com request = socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, ("216.58.204.78", 80)) ``` -------------------------------- ### Perform SOCKS5 username/password authentication Source: https://context7.com/sethmlarson/socksio/llms.txt Uses SOCKS5UsernamePasswordRequest to authenticate a connection. Requires an active socket and connection object. ```python def authenticate(sock, conn, username, password): """Perform SOCKS5 username/password authentication.""" request = socks5.SOCKS5UsernamePasswordRequest( username=username.encode(), password=password.encode() ) conn.send(request) sock.sendall(conn.data_to_send()) response = sock.recv(1024) reply = conn.receive_data(response) if reply.success: print("Authentication successful") return True else: print("Authentication failed - connection must be closed") return False ``` -------------------------------- ### Parse SOCKS4 Reply Source: https://context7.com/sethmlarson/socksio/llms.txt Demonstrates parsing raw SOCKS4 reply bytes and handling specific reply codes. ```python raw_reply = b"\x00\x5a\x00\x50\x00\x00\x00\x00" # Success reply reply = socks4.SOCKS4Reply.loads(raw_reply) print(f"Reply code: {reply.reply_code}") # SOCKS4ReplyCode.REQUEST_GRANTED print(f"Bound port: {reply.port}") # 80 print(f"Bound address: {reply.addr}") # 0.0.0.0 ``` ```python def handle_socks4_reply(reply): if reply.reply_code == socks4.SOCKS4ReplyCode.REQUEST_GRANTED: return "Connection established successfully" elif reply.reply_code == socks4.SOCKS4ReplyCode.REQUEST_REJECTED_OR_FAILED: return "Request rejected or failed" elif reply.reply_code == socks4.SOCKS4ReplyCode.CONNECTION_FAILED: return "Connection failed - cannot reach identd on client" elif reply.reply_code == socks4.SOCKS4ReplyCode.AUTHENTICATION_FAILED: return "Authentication failed - identd reports different user" ``` ```python # Handle malformed replies try: invalid_reply = socks4.SOCKS4Reply.loads(b"\x01\x5a\x00\x50") # Invalid format except ProtocolError as e: print(f"Protocol error: {e}") # "Malformed reply" ``` -------------------------------- ### SOCKS5Connection API Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks5.md Methods for managing the SOCKS5 connection state, sending requests, and receiving data from the proxy. ```APIDOC ## SOCKS5Connection ### Description Encapsulates a SOCKS5 connection, handling the packing of request objects and unpacking of reply data. ### Methods - **data_to_send()** -> bytes: Returns data to be sent via I/O and clears the buffer. - **receive_data(data: bytes)** -> SOCKS5AuthReply | SOCKS5Reply | SOCKS5UsernamePasswordReply: Unpacks raw response data into a reply object. - **send(request)** -> None: Packs a request object and adds it to the send buffer, progressing the protocol state. ### Properties - **state** (SOCKS5State): Returns the current state of the protocol. ``` -------------------------------- ### Create a SOCKS4 request Source: https://github.com/sethmlarson/socksio/blob/master/README.md Generates a request object for a SOCKS4 proxy using an IP address and port. ```python # SOCKS4 does not allow domain names, below is an IP for google.com request = socks4.SOCKS4Request.from_address( socks4.SOCKS4Command.CONNECT, ("216.58.204.78", 80)) ``` -------------------------------- ### SOCKS4Reply Class Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks4.md Represents a reply from the SOCKS4 proxy server. ```APIDOC ## class socksio.socks4.SOCKS4Reply(reply_code: SOCKS4ReplyCode, port: int, addr: str | None) ### Description Encapsulates a reply from the SOCKS4 proxy server. ### Parameters * **reply_code** (SOCKS4ReplyCode) - The code representing the type of reply. * **port** (int) - The port number returned. * **addr** (str | None) - Optional IP address returned. ### Methods #### classmethod loads(data: bytes) -> SOCKS4Reply Unpacks the reply data into an instance. * **Returns:** The unpacked reply instance. * **Raises:** **ProtocolError** – If the data does not match the spec. ``` -------------------------------- ### Parse and handle SOCKS5 replies Source: https://context7.com/sethmlarson/socksio/llms.txt Parses raw bytes into a SOCKS5Reply object and demonstrates mapping reply codes to human-readable messages. ```python from socksio import socks5, ProtocolError # Parse a successful reply raw_reply = b"\x05\x00\x00\x01\x7f\x00\x00\x01\x04\xd2" reply = socks5.SOCKS5Reply.loads(raw_reply) print(f"Reply code: {reply.reply_code}") # SUCCEEDED print(f"Address type: {reply.atype}") # IPV4_ADDRESS print(f"Bound address: {reply.addr}") # 127.0.0.1 print(f"Bound port: {reply.port}") # 1234 # Handle all reply codes def handle_socks5_reply(reply): codes = { socks5.SOCKS5ReplyCode.SUCCEEDED: "Success", socks5.SOCKS5ReplyCode.GENERAL_SERVER_FAILURE: "General server failure", socks5.SOCKS5ReplyCode.CONNECTION_NOT_ALLOWED_BY_RULESET: "Connection not allowed", socks5.SOCKS5ReplyCode.NETWORK_UNREACHABLE: "Network unreachable", socks5.SOCKS5ReplyCode.HOST_UNREACHABLE: "Host unreachable", socks5.SOCKS5ReplyCode.CONNECTION_REFUSED: "Connection refused", socks5.SOCKS5ReplyCode.TTL_EXPIRED: "TTL expired", socks5.SOCKS5ReplyCode.COMMAND_NOT_SUPPORTED: "Command not supported", socks5.SOCKS5ReplyCode.ADDRESS_TYPE_NOT_SUPPORTED: "Address type not supported", } return codes.get(reply.reply_code, "Unknown error") # Error handling for malformed replies try: socks5.SOCKS5Reply.loads(b"\x04\x00\x00\x01") # Wrong version except ProtocolError as e: print(f"Protocol error: {e}") ``` -------------------------------- ### Send application data through proxy Source: https://github.com/sethmlarson/socksio/blob/master/README.md Sends raw application data over the established proxy connection. ```python sock.sendall(b"GET / HTTP/1.1\r\nhost: google.com\r\n\r\n") data = receive_data(sock) print(data) # b'HTTP/1.1 301 Moved Permanently\r\nLocation: http://www.google.com/...` ``` -------------------------------- ### Handle SOCKS4 Reply Codes Source: https://context7.com/sethmlarson/socksio/llms.txt This snippet is intended to show how to handle SOCKS4 replies, but it is incomplete. It imports necessary classes for parsing proxy responses. ```python from socksio import socks4, ProtocolError ``` -------------------------------- ### Send Request to Connection Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Instructs the socksio connection object to prepare the request for sending. This method composes the necessary bytes according to the SOCKS protocol. ```python conn.send(request) ``` -------------------------------- ### SOCKS4Connection Class Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks4.md Manages a SOCKS4 and SOCKS4A connection, handling data packing and unpacking for requests and replies. ```APIDOC ## class socksio.socks4.SOCKS4Connection(user_id: bytes) ### Description Encapsulates a SOCKS4 and SOCKS4A connection. Packs request objects into data suitable to be send and unpacks reply data into their appropriate reply objects. ### Parameters * **user_id** (bytes) - The user ID to be sent as part of the requests. ### Methods #### data_to_send() -> bytes Returns the data to be sent via the I/O library of choice. Also clears the connection’s buffer. #### receive_data(data: bytes) -> SOCKS4Reply Unpacks response data into a reply object. * **Parameters:** * **data** (bytes) - The raw response data from the proxy server. * **Returns:** The appropriate reply object. #### send(request: SOCKS4Request | SOCKS4ARequest) -> None Packs a request object and adds it to the send data buffer. * **Parameters:** * **request** (SOCKS4Request | SOCKS4ARequest) - The request instance to be packed. ``` -------------------------------- ### Stop the SOCKS proxy container Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/development.md Stops and removes the Dante SOCKS server container. ```bash docker-compose -f docker/docker-compose.yml down ``` -------------------------------- ### SOCKS4Request Class Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks4.md Represents a request to a SOCKS4 proxy server, supporting IPv4 addresses. ```APIDOC ## class socksio.socks4.SOCKS4Request(command: SOCKS4Command, port: int, addr: bytes, user_id: bytes | None = None) ### Description Encapsulates a request to the SOCKS4 proxy server. ### Parameters * **command** (SOCKS4Command) - The command to request. * **port** (int) - The port number to connect to on the target host. * **addr** (bytes) - IP address of the target host. * **user_id** (bytes | None) - Optional user ID to be included in the request, if not supplied the user *must* provide one in the packing operation. ### Methods #### dumps(user_id: bytes | None = None) -> bytes Packs the instance into a raw binary in the appropriate form. * **Parameters:** * **user_id** (bytes | None) - Optional user ID as an override, if not provided the instance’s will be used, if none was provided at initialization an error is raised. * **Returns:** The packed request. * **Raises:** **SOCKSError** – If no user was specified in this call or on initialization. #### classmethod from_address(command: SOCKS4Command, address: str | bytes | Tuple[str | bytes, int], user_id: bytes | None = None) -> SOCKS4Request Convenience class method to build an instance from command and address. * **Parameters:** * **command** (SOCKS4Command) - The command to request. * **address** (str | bytes | Tuple[str | bytes, int]) - A string in the form ‘HOST:PORT’ or a tuple of ip address string and port number. * **user_id** (bytes | None) - Optional user ID. * **Returns:** A SOCKS4Request instance. * **Raises:** **SOCKSError** – If a domain name or IPv6 address was supplied. ``` -------------------------------- ### SOCKS4ARequest Class Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/api_socks4.md Represents a request to a SOCKS4A proxy server, supporting domain names. ```APIDOC ## class socksio.socks4.SOCKS4ARequest(command: SOCKS4Command, port: int, addr: bytes, user_id: bytes | None = None) ### Description Encapsulates a request to the SOCKS4A proxy server. ### Parameters * **command** (SOCKS4Command) - The command to request. * **port** (int) - The port number to connect to on the target host. * **addr** (bytes) - IP address of the target host. * **user_id** (bytes | None) - Optional user ID to be included in the request, if not supplied the user *must* provide one in the packing operation. ### Methods #### dumps(user_id: bytes | None = None) -> bytes Packs the instance into a raw binary in the appropriate form. * **Parameters:** * **user_id** (bytes | None) - Optional user ID as an override, if not provided the instance’s will be used, if none was provided at initialization an error is raised. * **Returns:** The packed request. * **Raises:** **SOCKSError** – If no user was specified in this call or on initialization. #### classmethod from_address(command: SOCKS4Command, address: str | bytes | Tuple[str | bytes, int], user_id: bytes | None = None) -> SOCKS4ARequest Convenience class method to build an instance from command and address. * **Parameters:** * **command** (SOCKS4Command) - The command to request. * **address** (str | bytes | Tuple[str | bytes, int]) - A string in the form ‘HOST:PORT’ or a tuple of ip address string and port number. * **user_id** (bytes | None) - Optional user ID. * **Returns:** A SOCKS4ARequest instance. ``` -------------------------------- ### Send Application Data Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md After a successful connection, this demonstrates sending raw HTTP request data through the established socket to the remote host via the proxy. ```python sock.sendall(b"GET / HTTP/1.1\r\nhost: google.com\r\n\r\n") ``` -------------------------------- ### Receive and Parse Proxy Reply Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Receives data from the SOCKS proxy via the socket and passes it to the socksio connection for parsing. This returns an event object representing the proxy's reply. ```python data = sock.recv(1024) event = conn.receive_data(data) ``` -------------------------------- ### Send Data to Proxy Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Retrieves the composed bytes from the socksio connection and sends them over the established socket to the SOCKS proxy. ```python data = conn.data_to_send() sock.sendall(data) ``` -------------------------------- ### Check SOCKS4 Reply Status Source: https://github.com/sethmlarson/socksio/blob/master/docs/source/usage.md Validates the reply code from the SOCKS proxy to ensure the connection request was granted. Raises an exception if the server could not connect to the remote host. ```python if event.reply_code != socks4.SOCKS4ReplyCode.REQUEST_GRANTED: raise Exception( "Server could not connect to remote host: {}".format(event.reply_code) ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.