Skip to main content

SocksLib

Sockslib is a library designed to make the usage of socks proxies as easy as possible. This library can connect to proxies, authenticate, and then have the use of a normal python socket.

Downloads PyPI version fury.io

Features

  • Socks5 support
  • Socks4 support
  • IPv4 Support
  • IPv6 Support (Socks5)
  • Domain Support
  • User/Pass authentication (Socks5)
  • Easily customizable authentication (Socks5)
  • UDP Support (Socks5)
  • Full socket api

Documentation

Index

  1. Intro
    1. Creating a socket
    2. Setting the proxy
    3. Set the default proxy
    4. URLLib proxied
    5. IPv6
  2. SOCKS5
    1. TCP
    2. UDP
    3. Using Other Authentication Methods
    4. Implementing Your Own Authentication Methods
  3. SOCKS4
    1. No Authentication
    2. Identity Authentication

Intro

Creating a new socket

socket = sockslib.SocksSocket()

Setting a proxy

socket.set_proxy (
	('127.0.0.1', 0),      # Ip, Port
	sockslib.Socks.SOCKS5, # SOCKS5/SOCKS4, (Optional) (Default SOCKS5)
	authentication         # Array of authentication methods (Optional) (Default NoAuth)
)

Set a default proxy

Default proxies can be useful for situations when other libraries use sockets to communicate and you want to force that library to use a proxy instead of a direct connection

sockslib.set_default_proxy(
	('127.0.0.1', 0),      # Ip, Port
	sockslib.Socks.SOCKS5, # SOCKS5/SOCKS4, (Optional) (Default SOCKS5)
	socket.AF_INET,        # Default protocol (AF_INET, AF_INET6)
	authentication         # Array of authentication methods (Optional) (Default NoAuth)
)

URLLib Proxied

import urllib.request

import sockslib
import socket

sockslib.set_default_proxy(('127.0.0.1', 9050)) # Set the default proxy
socket.socket = sockslib.SocksSocket # Make the default socket object a SocksSocket

html = urllib.request.urlopen('https://myexternalip.com/raw').read() # Request the page
print(html)

IPv6

To connect to a proxy server via ipv6, you must explicitly specify the protocol by passing the ip_protocol parameter to the SocksSocket constructor, OR by using the 3rd option of set_default_proxy. Here is an example of connecting to a remote IPv6 proxy:

import sockslib
import socket # import socket for the protocol specifiers 

with sockslib.SocksSocket(ip_protocol=socket.AF_INET6) as sock:
    sock.set_proxy(('::1', 9050)) # Set proxy

    sock.connect(('myexternalip.com', 80)) # Connect to Server via proxy 
    sock.sendall(b"GET /raw HTTP/1.1\r\nHost: myexternalip.com\r\n\r\n") # Send HTTP Request
    print(sock.recv(1024)) # Print response

Examples

SOCKS5

Socks5 TCP

This is an example usage that connects to a Socks5 proxy at 127.0.0.1:9050 and then requests the page http://myexternalip.com/raw

import sockslib

with sockslib.SocksSocket() as sock:
    sock.set_proxy(('127.0.0.1', 9050)) # Set proxy

    sock.connect(('myexternalip.com', 80)) # Connect to Server via proxy
    sock.sendall(b"GET /raw HTTP/1.1\r\nHost: myexternalip.com\r\n\r\n") # Send HTTP Request
    print(sock.recv(1024)) # Print response

Socks5 UDP

This is an example usage that connects to a Socks5 proxy at 127.0.0.1:9050 and then sends a UDP packet.

import sockslib

with sockslib.SocksSocket(udp=True) as sock:
    sock.set_proxy(('127.0.0.1', 9050)) # Set proxy

    sock.initudp() # Connect to Server and bind a UDP port for communication
    sock.sendto(b"Hello, World", ("0.0.0.0", 12000)) # Send "Hello, World" to 0.0.0.0:12000 over UDP

Due to the nature of UDP in the SOCKS5 protocol, it is reccomended to use the context manager, as the remote proxy server will not drop the UDP associate request until the socket is closed.

Using other authentication methods

To use more authentication methods like User/Pass auth, you pass an array of authentication methods to the third parameter of set_proxy (Don't neglect to set the second parameter to the proxy type!)

import sockslib

with sockslib.SocksSocket() as sock:
    auth_methods = [
        sockslib.NoAuth(),                             # No authentication
        sockslib.UserPassAuth('username', 'password'), # Username / Password authentication
    ]
    sock.set_proxy(('127.0.0.1', 9050), sockslib.Socks.SOCKS5, auth_methods) # Set proxy

    sock.connect(('myexternalip.com', 80)) # Connect to Server via proxy
    sock.sendall(b"GET /raw HTTP/1.1\r\nHost: myexternalip.com\r\n\r\n") # Send HTTP Request
    print(sock.recv(1024)) # Print response

Implementing your own authentication methods

To implement your own socks5 authentication method, you must make a class that implements sockslib.AuthenticationMethod it requires that you implement a getId() function, an authenticate(socket) function, and a forP function. Note: the authenticate function must return a boolean, True if authentication succeeded and False if it failed.

from sockslib.socks import AuthenticationMethod
from sockslib.socks import Socks
import struct

class UserPassAuth(AuthenticationMethod):
    def __init__(self, username, password):
        self.username = username
        self.password = password

    def getId(self):
        return 0x02 # 0x02 means password authentication, see https://en.wikipedia.org/wiki/SOCKS#SOCKS5 for more

    def forP(self):
        return Socks.SOCKS5 # For SOCKS5

    def authenticate(self, socket):
        socket.sendall(b"\x01" + struct.pack("B", len(self.username)) + self.username.encode() + struct.pack("B", len(self.password)) + self.password.encode()) # Send authentication packet
        ver, status = socket.recv(2) # Get authentication response

        return status == 0x00

Socks4

This is an example usage that connects to a Socks4 proxy at 127.0.0.1:9050 and then requests the page http://myexternalip.com/raw

import sockslib

with sockslib.SocksSocket() as sock:
    sock.set_proxy(('127.0.0.1', 9050), sockslib.Socks.SOCKS4) # Set proxy

    sock.connect(('myexternalip.com', 80)) # Connect to Server via proxy
    sock.sendall(b"GET /raw HTTP/1.1\r\nHost: myexternalip.com\r\n\r\n") # Send HTTP Request
    print(sock.recv(1024)) # Print response

Socks4 with identity authentication

import sockslib

with sockslib.SocksSocket() as sock:
    auth_methods = [
        sockslib.Socks4Ident("ident")
    ]
    sock.set_proxy(('127.0.0.1', 9050), sockslib.Socks.SOCKS4, auth_methods) # Set proxy

    sock.connect(('myexternalip.com', 80)) # Connect to Server via proxy
    sock.sendall(b"GET /raw HTTP/1.1\r\nHost: myexternalip.com\r\n\r\n") # Send HTTP Request
    print(sock.recv(1024)) # Print response

Installation

pip3 install sockslib

Issues

If you have any issues with this project please feel free to open a new issue on github https://github.com/woo200/sockslib/issues

Release files for sockslib 1.7.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sockslib 1.7.6
File Size Uploaded
sockslib-1.7.6.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sockslib 1.7.6
File Interpreter ABI Platform
sockslib-1.7.6-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / sockslib-1.7.6.tar.gz

Download URL sockslib-1.7.6.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a7a7797b29a24706cd4cbd27c3c17da528b89bdf8378ce38cd8f54736f6a4a1b
BLAKE2b-256 checksum
How to use checksums
be0c31bf1bd00cf9337c3329c7e144e1b1e9e286b4902cf341e8c8a10245c7fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.5

Release files / sockslib-1.7.6-py3-none-any.whl

Download URL sockslib-1.7.6-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b667b6565c7a7ffa82c77dd7e8f4b775c6a8aa815e0fe1d0f397c30b1cf67a12
BLAKE2b-256 checksum
How to use checksums
a27f2c05b5fa2f858c608e768e84657f979ccf7eeaac24ca246787feec56561a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.5

Release history Release notifications | RSS feed

This release

1.7.6 This release

2 release files

1.7.5

2 release files

1.7.4

2 release files

1.7.3

2 release files

1.7.2

2 release files

1.7.1

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.2

2 release files

1.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page