代理列表格式转换问题:用户名密码位置错误求助
代理格式转换代码修复
原代码的问题在于使用socks.join()的方式错误,导致用户名密码被错误插入socks5前缀,且位置不符合要求。正确的做法是拆分IP和端口后,按顺序拼接所需字段。
修复后的代码:
file = "proxys.txt" socks_type = "socks5" oauth = input("Enter Username and Password. If no auth leave blank: ").strip() with open(file, 'r') as fp: formatted_proxies = [] for line in fp.readlines(): line = line.strip() if not line: continue # 拆分IP和端口 ip, port = line.split(':') # 构建基础字段列表 proxy_parts = [socks_type, ip, port] # 如果有用户名密码,添加到列表末尾 if oauth: proxy_parts.extend(oauth.split()) # 拼接成目标格式并添加换行符 formatted_proxies.append(' '.join(proxy_parts) + '\n') with open(file, 'w') as fp: fp.writelines(formatted_proxies)
代码说明
- 读取每行代理时先去除多余空格和换行符,拆分出独立的IP与端口字段
- 按
socks5 IP PORT的基础格式构建字段列表 - 若输入了用户名密码,将其拆分后追加到列表末尾,保证位置正确
- 用空格拼接所有字段,生成符合要求的代理格式
内容的提问来源于stack exchange,提问作者Don Schulz
相关产品推荐
相关产品推荐

