File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import re
2+
3+ proxy_strings = [
4+ '127.0.0.1:8000:username:password' ,
5+ 'username:password@127.0.0.1:8000' ,
6+ 'https://username:password@127.0.0.1:8000' ,
7+ 'socks5://username:password@127.0.0.1:8000' ,
8+ 'socks4://username:password@127.0.0.1:8000' ,
9+ 'example.com:8080' ,
10+ '192.168.1.1:9000' ,
11+ 'username:password@example.com:8080'
12+ ]
13+
14+ def reformat_proxy (proxy_str ):
15+ prefixes = ("http://" , "socks4://" , "socks5://" )
16+
17+ proxy_str = proxy_str .replace ("https://" , "http://" ) # in case always using http://
18+ if proxy_str .startswith (prefixes ):
19+ return proxy_str
20+
21+ if "@" in proxy_str :
22+ return f"http://{ proxy_str } "
23+
24+ match = re .search (r'([\d.]+|[a-zA-Z0-9.-]+):(\d+)' , proxy_str )
25+ if match :
26+ hostname , port = match .groups ()
27+ hostname = f"{ hostname } :{ port } "
28+
29+ if proxy_str .index (hostname ) == 0 :
30+ proxy_str = proxy_str .replace (f"{ hostname } :" , "" ).replace (f"{ hostname } " , "" )
31+ return f"http://{ proxy_str + '@' if proxy_str else '' } { hostname } "
32+
33+ raise Exception ("Invalid proxy format" )
34+
35+ for proxy_str in proxy_strings :
36+ print (reformat_proxy (proxy_str ))
You can’t perform that action at this time.
0 commit comments