Created
March 9, 2024 10:37
-
-
Save jpcaparas/4c67c3b4f2aac698cfb3ff9d66b0936f to your computer and use it in GitHub Desktop.
Python 5 concurrent SOCKS5 requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import requests | |
import threading | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
from itertools import cycle | |
def make_request(server, socks5_credentials, semaphore): | |
username, password, port = socks5_credentials.split(':') | |
proxies = { | |
'http': f'socks5://{username}:{password}@{server}:{port}', | |
'https': f'socks5://{username}:{password}@{server}:{port}', | |
} | |
try: | |
with semaphore: | |
response = requests.get('https://httpbin.org/get', proxies=proxies) | |
response.raise_for_status() # Raise an exception for HTTP errors | |
data = response.json() | |
print(f"Response from {server}:\n{data}\n") | |
except requests.RequestException as e: | |
print(f"Request failed: {e}") | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Make concurrent HTTP requests to httpbin.org/get using SOCKS5 credentials.') | |
parser.add_argument('--username', type=str, required=True, help='SOCKS5 username') | |
parser.add_argument('--password', type=str, required=True, help='SOCKS5 password') | |
parser.add_argument('--port', type=int, required=True, help='SOCKS5 port') | |
parser.add_argument('--server', action='append', required=True, | |
help='Server address, can be used multiple times for different servers') | |
parser.add_argument('--count', type=int, help='Total number of requests to perform') | |
parser.add_argument('--concurrency', type=int, default=1, help='Number of concurrent requests') | |
args = parser.parse_args() | |
# Prepare servers and credentials | |
if not args.server: | |
print("At least one server must be specified.") | |
return | |
if not args.count: | |
args.count = len(args.server) | |
servers_cycle = cycle(args.server) # Cycle through servers if necessary | |
creds = f"{args.username}:{args.password}:{args.port}" | |
tasks = [] | |
# Threading setup | |
semaphore = threading.Semaphore(args.concurrency) # Control concurrency | |
with ThreadPoolExecutor(max_workers=args.concurrency) as executor: | |
for _ in range(args.count): | |
server = next(servers_cycle) | |
tasks.append(executor.submit(make_request, server, creds, semaphore)) | |
# Wait for all tasks to complete | |
for future in as_completed(tasks): | |
pass | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python script.py \ | |
--username <username> \ | |
--password <password> \ | |
--port 1080 \ | |
--server amsterdam.nl.socks.nordhold.net \ | |
--server atlanta.us.socks.nordhold.net \ | |
--server dallas.us.socks.nordhold.net \ | |
--count 20 \ | |
--concurrency 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment