简体   繁体   English

如何使用代理服务器创建套接字?

[英]How do I create a socket using a proxy server?

import socket
sock = socket.socket()
sock.connect('somesite')

If I wanted the socket connect through a proxy, how would I do this? 如果我想通过代理连接套接字,我该怎么办? I don't see a relevant parameter or option in the docs. 我在文档中没有看到相关的参数或选项。

You first need to figure out what kind of proxy it is (SOCKS, HTTP, etc), and then either find a library that implements connections for that proxy type, or roll your own routine. 您首先需要确定它是哪种代理(SOCKS,HTTP等),然后找到实现该代理类型的连接的库,或者滚动您自己的例程。 The basic idea is you'll have your script connect to the proxy server/port, and then request the proxy to connect to the site you're trying to request. 基本思想是使脚本连接到代理服务器/端口,然后请求代理连接到您要请求的站点。

If you don't want to do it like that, another viable option would be to use LD_PRELOAD to get it to connect transparently (under Linux), like torify does. 如果您不想那样做,那么另一个可行的选择是使用LD_PRELOAD使其透明连接(在Linux下),就像torify一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM