繁体   English   中英

提升 python vpn 服务器

[英]Raising a python vpn server

我想在 python 中创建一个 vpn 服务器,我找到了这个解决方案: http://voorloopnul.com/blog/a-python-proxy-in-less-than-100-lines-of-code/

但是,它适用于Python 2 ,我使用python 3 因此,我重写了一些代码,结果是这样的:

import socket
from select import select
import sys
import logging
from this import s



class TcpTee:

    def __init__(self, source_port, destination):
        self.destination = destination

        self.teesock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.teesock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.teesock.bind(('127.0.0.1', source_port))
        self.teesock.listen(200)

        # Linked client/server sockets in both directions
        self.channel = {}

    def run(self):
        while 1:
            inputready, outputready, exceptready = select([self.teesock] + self.channel.keys(), [], [])
            for s in inputready:
                if s == self.teesock:
                    self.on_accept()
                    break

                data = s.recv(4096)
                if not data:
                    self.on_close(s)
                    break

                self.on_recv(s, data)

    def on_accept(self):
        clientsock, clientaddr = self.teesock.accept()
        serversock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            serversock.connect(self.destination)
        except Exception:
            logging.exception(
                'Could not connect to server %s. Closing connection to client %s' % (self.destination, clientaddr))
            clientsock.close()
        else:
            logging.info("%r has connected", clientaddr)
            self.channel[clientsock] = serversock
            self.channel[serversock] = clientsock

    def on_close(self, sock):
        logging.info("%s has disconnected", s.getpeername())
        othersock = self.channel[sock]

        sock.close()
        othersock.close()

        del self.channel[sock]
        del self.channel[othersock]

    def on_recv(self, sock, data):
        print(data)
        self.channel[sock].send(data)



import argparse

parser = argparse.ArgumentParser()
parser.add_argument("listen_port", help="The port this process will listen on.", type=int)
parser.add_argument("server_host", help="The remote host to connect to.")
parser.add_argument("server_port", help="The remote port to connect to.", type=int)
args = parser.parse_args()

logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

tee = TcpTee(int(args.listen_port), (args.server_host, int(args.server_port)))
try:
    tee.run()
except KeyboardInterrupt:
    logging.info("Ctrl C - Good Bye")
    sys.exit(1)

最初,启动时没有发生任何事情,但经过少量编辑后,它开始出现错误:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
usage: vpn.py [-h] listen_port server_host server_port
vpn.py: error: the following arguments are required: listen_port, server_host, server_port

Process finished with exit code 2

从来没有遇到过这样的问题如何纠正? 或者也许有人已经有了这个实现

错误信息

usage: vpn.py [-h] listen_port server_host server_port
vpn.py: error: the following arguments are required: listen_port, server_host, server_port

Process finished with exit code 2

(忽略不必要的this导入所产生的麻烦)表明您没有使用所需的 arguments 运行脚本。 也就是说,你跑了

python vpn.py

而不是类似的东西

python vpn.py 12345 somewhere.com 23456

import this时出现 python 的 zen 。 因此,您的代码存在以下问题:

from this import s

暂无
暂无

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

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