【问题标题】:Creating a python proxy server [closed]创建一个python代理服务器[关闭]
【发布时间】:2020-05-22 10:41:44
【问题描述】:

大家好,我正在尝试使用 python 编程语言创建代理服务器。

我在http://voorloopnul.com/blog/a-python-proxy-in-less-than-100-lines-of-code/ 找到了这样的解决方案,虽然问题是脚本是用 Python 2.7 版本编写的,但我是用 Python 3.0 版本编写的。

在重写脚本以提供我需要的 3.0 版本后,它仍然无法正常工作。

我会很快解释我正在尝试做什么,所有内容都显示在这张图片中:

它们之间有用户-网站和代理服务器。

如果建议,该代理服务器 - 是具有 Windows 操作系统的常规计算机,在其上启动此脚本。用户知道代理服务器的 IP 地址,在浏览器 Firefox 的设置中输入它并移动到该网站上的网站 myip.com 显示服务器的 IP 地址 - 这是该脚本的正确操作,但我的作品都没有。或许这个问题有现成的解决方案?

【问题讨论】:

  • 您的问题可以改进。您是否看到您链接的代码顶部有一个电子邮件地址?建议您联系原作者。
  • 您能澄清一下您要做什么吗?对我来说,不清楚您是否尝试创建反向代理:到 单个服务器 的所有连接都必须通过代理或转发代理:从客户端到 _any 服务器的所有连接都必须通过代理
  • @Minion3665 从客户端到_任何服务器的所有连接都必须通过代理
  • @Apelsin2020 您可以尝试使用 2to3 将程序从 python2 转换为 python3。当我这样做时,脚本似乎可以工作
  • @minion3665 如果可能,请在答案中显示代码。但据我了解,此代码允许您连接到最终的单个服务器,而不是所有站点。也许我在混淆一些东西

标签: python python-3.x proxy


【解决方案1】:

几年前我用this做了一个服务器

def __init__(self, config):
    # Shutdown on Ctrl+C
    signal.signal(signal.SIGINT, self.shutdown) 

    # Create a TCP socket
    self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Re-use the socket
    self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # bind the socket to a public host, and a port   
    self.serverSocket.bind((config['HOST_NAME'], config['BIND_PORT']))

    self.serverSocket.listen(10) # become a server socket
    self.__clients = {}

while True:

    # Establish the connection
    (clientSocket, client_address) = self.serverSocket.accept() 

    d = threading.Thread(name=self._getClientName(client_address), 
    target = self.proxy_thread, args=(clientSocket, client_address))
    d.setDaemon(True)
    d.start()

# get the request from browser
request = conn.recv(config['MAX_REQUEST_LEN']) 

# parse the first line
first_line = request.split('\n')[0]

# get url
url = first_line.split(' ')[1]


http_pos = url.find("://") # find pos of ://
if (http_pos==-1):
    temp = url
else:
    temp = url[(http_pos+3):] # get the rest of url

port_pos = temp.find(":") # find the port pos (if any)

# find end of web server
webserver_pos = temp.find("/")
if webserver_pos == -1:
    webserver_pos = len(temp)

webserver = ""
port = -1
if (port_pos==-1 or webserver_pos < port_pos): 

    # default port 
    port = 80 
    webserver = temp[:webserver_pos] 

else: # specific port 
    port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
    webserver = temp[:port_pos] 


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.settimeout(config['CONNECTION_TIMEOUT'])
s.connect((webserver, port))
s.sendall(request)

while 1:
    # receive data from web server
    data = s.recv(config['MAX_REQUEST_LEN'])

    if (len(data) > 0):
        conn.send(data) # send to browser/client
    else:
        break

【讨论】:

  • 兄弟你能告诉我conn是什么吗?之前用在哪里?
猜你喜欢
  • 2012-07-12
  • 1970-01-01
  • 2012-03-10
  • 2014-02-22
  • 1970-01-01
  • 2010-11-02
  • 2013-05-03
  • 1970-01-01
相关资源
最近更新 更多