本文实例讲述了Python实现简单的代理服务器。分享给大家供大家参考。具体如下:
具备简单的管理功能,运行后 telnet localhost 9000 端口可以进行管理主要功能就是做包转发,如果有一个桥服务器,可以用来外网访问内网用,还是很管用的
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#/bin/env python #coding:utf-8 import socket,select,sys,time import thread s_list = [] def loop(cs,addr,s_ip,s_port): print '%s %d connected.' % addr ts = socket.socket() try : ts.connect((s_ip,s_port)) except : cs.close() print '%s %d closed.' % addr sys.exit( 0 ) while True : rl,wl,xl = select.select([cs.fileno(),ts.fileno()],[],[cs.fileno(),ts.fileno()]) if len (xl) > 0 : cs.close() ts.close() print '%s %d closed.' % addr sys.exit( 0 ) if len (rl) > 0 : if rl[ 0 ] = = cs.fileno(): rs = ts ws = cs else : rs = cs ws = ts try : buffer = ws.recv( 10000 ) if len ( buffer ) = = 0 : raise rs.send( buffer ) except : rs.close() ts.close() print '%s %d closed.' % addr sys.exit( 0 ) def mainserver(l_port,s_ip,s_port): global s_list try : ss = socket.socket() ss.bind(( '0.0.0.0' ,l_port)) ss.listen( 10 ) s_list.append((l_port,s_ip,s_port)) except : sys.exit( 0 ) while True : cs,addr = ss.accept() thread.start_new_thread(loop,(cs,addr,s_ip,s_port)) def manager(l_port): global start,s_list ss = socket.socket() ss.bind(( '0.0.0.0' ,l_port)) ss.listen( 10 ) while True : cs,addr = ss.accept() cs.send( """trans server 1.0\r\ntype 'help' to get help\r\n""" ) buffer = '' while True : buf = cs.recv( 10000 ) if len (buf) = = 0 : cs.close() break if buf[ - 1 ] not in ( '\r' , '\n' ): buffer + = buf continue buffer + = buf cmd = buffer .strip() buffer = '' if cmd = = 'exit' : cs.close() break elif cmd = = 'stop' : start = 0 cs.close() sys.exit( 0 ) elif cmd = = 'list' : b = '' for l in s_list: b + = '%4d %s:%d\r\n' % l if len (b) > 0 : cs.send(b) elif cmd in ( 'help' , '?' ): cs.send( """-------------------------------------------\r exit\r exit telnet\r start localport serverip:serverport\r start a new server\r list\r list all server\r -------------------------------------------\r """ ) else : cmds = cmd.split( " " , 1 ) if len (cmds) > 1 and cmds[ 0 ] = = 'start' : args = cmds[ 1 ].strip().split( " " , 1 ) if len (args) ! = 2 : cs.send( 'start localport serverip:serverport\r\n' ) continue arg = args[ 1 ].split( ":" , 1 ) if len (arg) ! = 2 : cs.send( 'start localport serverip:serverport\r\n' ) continue try : l_port = int (args[ 0 ]) s_ip = arg[ 0 ] s_port = int (arg[ 1 ]) except : cs.send( 'start localport serverip:serverport\r\n' ) continue thread.start_new_thread(mainserver,(l_port,s_ip,s_port)) cs.send( 'start OK!\r\n' ) else : cs.send( 'no command [%s]\r\n' % cmd) continue def main(): global start if len (sys.argv) = = 3 : try : l_port = int (sys.argv[ 1 ]) s_ip,s_port = sys.argv[ 2 ].split( ":" ) s_port = int (s_port) thread.start_new_thread(mainserver,(l_port,s_ip,s_port)) except : pass start = 1 thread.start_new_thread(manager,( 9000 ,)) while start: time.sleep( 1 ) if __name__ = = '__main__' : start = 0 main() |
希望本文所述对大家的Python程序设计有所帮助。