脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 尝试用最短的Python代码来实现服务器和代理服务器

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

2020-08-29 10:45mattkang Python

"人生苦短、我用Python",Python最大的特点便是代码简洁,得益于Python自带的wsgiref包和socket模块,这里我们将尝试用最短的Python代码来实现服务器和代理服务器:

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

?
1
2
3
4
5
6
7
8
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!\n']
httpd = make_server('', 80, simple_app)
httpd.serve_forever()

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

 

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到的数据原封不动的传递
下面上代码。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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)

效果截图如下。

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

延伸 · 阅读

精彩推荐