File: example_socks4.py

package info (click to toggle)
socksio 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: python: 1,117; makefile: 12; sh: 12
file content (44 lines) | stat: -rw-r--r-- 1,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import socket

from socksio import socks4


def send_data(sock, data):
    print("sending:", data)
    sock.sendall(data)


def receive_data(sock):
    data = sock.recv(1024)
    print("received:", data)
    return data


def main():
    # Assuming a running SOCKS4 proxy running in localhost:1080
    sock = socket.create_connection(("localhost", 1080))
    conn = socks4.SOCKS4Connection(user_id=b"socksio")

    # Request to connect to google.com port 80
    # 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)  # or "216.58.204.78:80"
    )
    conn.send(request)
    send_data(sock, conn.data_to_send())
    data = receive_data(sock)
    event = conn.receive_data(data)
    print("Request reply:", event)
    if event.reply_code != socks4.SOCKS4ReplyCode.REQUEST_GRANTED:
        raise Exception(
            "Server could not connect to remote host: {}".format(event.reply_code)
        )

    # Send an HTTP request to the connected proxy
    sock.sendall(b"GET / HTTP/1.1\r\nhost: google.com\r\n\r\n")
    data = receive_data(sock)
    print(data)


if __name__ == "__main__":
    main()