如何在 Python 中进行 VPN/代理连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35144685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:03:20  来源:igfitidea点击:

How to VPN/Proxy connect in Python?

pythonvpnpythonanywherepia

提问by Liam Flynn

I'm trying to scrape some pages that are on a website but to view the pages, I need to be connected to a VPN. My setup is as follows:

我正在尝试抓取网站上的一些页面,但要查看这些页面,我需要连接到 VPN。我的设置如下:

I want to run a script on pythonanywhere that connects through the VPN so that the traffic appears to be coming from Australia.

我想在通过 VPN 连接的 pythonanywhere 上运行一个脚本,以便流量似乎来自澳大利亚。

The closest answer I have found so far is:

到目前为止,我找到的最接近的答案是:

JSoup over VPN/proxy

基于 VPN/代理的 JSoup

回答by Alexey Smirnov

I see that https://www.privateinternetaccess.com/has option to use SOCKS5 proxy. If you are using requests module for scraping you may use SOCKS5 like that:

我看到 https://www.privateinternetaccess.com/可以选择使用 SOCKS5 代理。如果您使用 requests 模块进行抓取,您可以像这样使用 SOCKS5:

pip install -U requests[socks]

and in the script:

并在脚本中:

import requests
proxies = {'http': 'socks5://user:pass@host:port',
           'https': 'socks5://user:pass@host:port'}

resp = requests.get('http://example.com', proxies=proxies )

回答by Vin

You should be specific about what you want. A proxy is different from a VPN.

你应该具体说明你想要什么。代理不同于 VPN。

A proxy is easier to create in Python.

在 Python 中创建代理更容易。

Here's the code:

这是代码:

import socket
import select
import time
import sys

# Changing the buffer_size and delay, you can improve the speed and bandwidth.
# But when buffer get to high or delay go too down, you can broke things
buffer_size = 4096
delay = 0.0001
forward_to = ('smtp.zaz.ufsk.br', 25)

class Forward:
    def __init__(self):
        self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def start(self, host, port):
        try:
            self.forward.connect((host, port))
            return self.forward
        except Exception, e:
            print e
            return False

class TheServer:
    input_list = []
    channel = {}

    def __init__(self, host, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((host, port))
        self.server.listen(200)

    def main_loop(self):
        self.input_list.append(self.server)
        while 1:
            time.sleep(delay)
            ss = select.select
            inputready, outputready, exceptready = ss(self.input_list, [], [])
            for self.s in inputready:
                if self.s == self.server:
                    self.on_accept()
                    break

                self.data = self.s.recv(buffer_size)
                if len(self.data) == 0:
                    self.on_close()
                    break
                else:
                    self.on_recv()

    def on_accept(self):
        forward = Forward().start(forward_to[0], forward_to[1])
        clientsock, clientaddr = self.server.accept()
        if forward:
            print clientaddr, "has connected"
            self.input_list.append(clientsock)
            self.input_list.append(forward)
            self.channel[clientsock] = forward
            self.channel[forward] = clientsock
        else:
            print "Can't establish connection with remote server.",
            print "Closing connection with client side", clientaddr
            clientsock.close()

    def on_close(self):
        print self.s.getpeername(), "has disconnected"
        #remove objects from input_list
        self.input_list.remove(self.s)
        self.input_list.remove(self.channel[self.s])
        out = self.channel[self.s]
        # close the connection with client
        self.channel[out].close()  # equivalent to do self.s.close()
        # close the connection with remote server
        self.channel[self.s].close()
        # delete both objects from channel dict
        del self.channel[out]
        del self.channel[self.s]

    def on_recv(self):
        data = self.data
        # here we can parse and/or modify the data before send forward
        print data
        self.channel[self.s].send(data)

if __name__ == '__main__':
        server = TheServer('', 9090)
        try:
            server.main_loop()
        except KeyboardInterrupt:
            print "Ctrl C - Stopping server"
            sys.exit(1)

Hope you found it useful ;-)

希望你觉得它有用;-)