尝试用最短的Python代码来实现服务器和代理服务器

一个最简单的服务器
Python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行。

from wsgiref.simple_server import make_server def simple_app(environ, start_response):   status = '200 OK'   response_headers = [('Content-type','text/plain')]   start_response(status, response_headers)   return ['Hello world!'] httpd = make_server('', 80, simple_app) httpd.serve_forever() 

登录后复制

2016623145600293.jpg (247×67)

50行代码实现代理服务器
之前遇到一个场景是这样的:
我在自己的电脑上需要用mongodb图形客户端,但是mongodb的服务器地址没有对外网开放,只能通过先登录主机A,然后再从A连接mongodb服务器B。
本来想通过ssh端口转发的,但是我没有从机器A连接ssh到B的权限。于是就自己用Python写一个。
原理很简单。
1.开一个socket server监听连接请求
2.每接受一个客户端的连接请求,就往要转发的地址建一条连接请求。即client->proxy->forward。proxy既是socket服务端(监听client),也是socket客户端(往forward请求)。
3.把client->proxy和proxy->forward这2条socket用字典给绑定起来。
4.通过这个映射的字典把send/recv到的数据原封不动的传递
下面上代码。

#coding=utf-8 import socket import select import sys  to_addr = ('xxx.xxx.xx.xxx', 10000)#转发的地址  class Proxy:   def __init__(self, addr):     self.proxy = socket.socket(socket.AF_INET,socket.SOCK_STREAM)     self.proxy.bind(addr)     self.proxy.listen(10)     self.inputs = [self.proxy]     self.route = {}    def serve_forever(self):     print 'proxy listen...'     while 1:       readable, _, _ = select.select(self.inputs, [], [])       for self.sock in readable:         if self.sock == self.proxy:           self.on_join()         else:           data = self.sock.recv(8096)           if not data:             self.on_quit()           else:             self.route[self.sock].send(data)    def on_join(self):     client, addr = self.proxy.accept()     print addr,'connect'     forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     forward.connect(to_addr)     self.inputs += [client, forward]     self.route[client] = forward     self.route[forward] = client    def on_quit(self):     for s in self.sock, self.route[self.sock]:       self.inputs.remove(s)       del self.route[s]       s.close()  if __name__ == '__main__':   try:     Proxy(('',12345)).serve_forever()#代理服务器监听的地址   except KeyboardInterrupt:     sys.exit(1) 

登录后复制

效果截图如下。

2016623145635171.jpg (574×128)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2286091.html

(0)
上一篇 2025年2月27日 21:52:18
下一篇 2025年2月27日 21:53:24

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论