1
- #!/usr/bin/env python
2
- #coding:utf-8
1
+ # -*- coding: utf-8 -*-
3
2
4
- '''filename:rtcp.py
3
+ '''
4
+ filename:rtcp.py
5
5
@desc:
6
6
利用python的socket端口转发,用于远程维护
7
7
如果连接不到远程,会sleep 36s,最多尝试200(即两小时)
17
17
@date: 2009-7
18
18
'''
19
19
20
- import threading
21
20
import socket
22
- import sys ,os ,time
21
+ import sys
22
+ import threading
23
+ import time
23
24
24
- streams = [None ,None ] #存放需要进行数据转发的两个数据流(都是SocketObj对象)
25
- debug = 1 # 调试状态 0 or 1
25
+ streams = [None , None ] # 存放需要进行数据转发的两个数据流(都是SocketObj对象)
26
+ debug = 1 # 调试状态 0 or 1
26
27
27
28
def _usage ():
28
29
print 'Usage: ./rtcp.py stream1 stream2\n stream : l:port or c:host:port'
@@ -48,7 +49,7 @@ def _get_another_stream(num):
48
49
else :
49
50
time .sleep (1 )
50
51
51
- def _xstream (num ,s1 ,s2 ):
52
+ def _xstream (num , s1 , s2 ):
52
53
'''
53
54
交换两个流的数据
54
55
num为当前流编号,主要用于调试目的,区分两个回路状态用。
@@ -82,65 +83,65 @@ def _xstream(num,s1,s2):
82
83
83
84
streams [0 ] = None
84
85
streams [1 ] = None
85
- print num ,"CLOSED"
86
+ print num , "CLOSED"
86
87
87
- def _server (port ,num ):
88
+ def _server (port , num ):
88
89
'''
89
90
处理服务情况,num为流编号(第0号还是第1号)
90
91
'''
91
- srv = socket .socket (socket .AF_INET ,socket .SOCK_STREAM )
92
- srv .bind (('0.0.0.0' ,port ))
92
+ srv = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
93
+ srv .bind (('0.0.0.0' , port ))
93
94
srv .listen (1 )
94
- while 1 :
95
- conn ,addr = srv .accept ()
96
- print "connected from:" ,addr
97
- streams [num ] = conn #放入本端流对象
98
- s2 = _get_another_stream (num ) # 获取另一端流对象
99
- _xstream (num ,conn ,s2 )
95
+ while True :
96
+ conn , addr = srv .accept ()
97
+ print "connected from:" , addr
98
+ streams [num ] = conn # 放入本端流对象
99
+ s2 = _get_another_stream (num ) # 获取另一端流对象
100
+ _xstream (num , conn , s2 )
100
101
101
- def _connect (host ,port ,num ):
102
- ''' 处理连接, num为流编号(第0号还是第1号)
102
+ def _connect (host , port , num ):
103
+ ''' 处理连接, num为流编号(第0号还是第1号)
103
104
104
105
@note: 如果连接不到远程,会sleep 36s,最多尝试200(即两小时)
105
106
'''
106
107
not_connet_time = 0
107
108
wait_time = 36
108
109
try_cnt = 199
109
- while 1 :
110
+ while True :
110
111
if not_connet_time > try_cnt :
111
112
streams [num ] = 'quit'
112
113
print ('not connected' )
113
114
return None
114
115
115
- conn = socket .socket (socket .AF_INET ,socket .SOCK_STREAM )
116
+ conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
116
117
try :
117
- conn .connect ((host ,port ))
118
+ conn .connect ((host , port ))
118
119
except Exception , e :
119
- print ('can not connect %s:%s!' % (host ,port ))
120
+ print ('can not connect %s:%s!' % (host , port ))
120
121
not_connet_time += 1
121
122
time .sleep (wait_time )
122
123
continue
123
124
124
- print "connected to %s:%i" % (host ,port )
125
+ print "connected to %s:%i" % (host , port )
125
126
streams [num ] = conn #放入本端流对象
126
127
s2 = _get_another_stream (num ) #获取另一端流对象
127
- _xstream (num ,conn ,s2 )
128
+ _xstream (num , conn , s2 )
128
129
129
130
130
131
if __name__ == '__main__' :
131
132
if len (sys .argv ) != 3 :
132
133
_usage ()
133
134
sys .exit (1 )
134
- tlist = [] # 线程列表,最终存放两个线程对象
135
- targv = [sys .argv [1 ],sys .argv [2 ] ]
136
- for i in [0 ,1 ]:
137
- s = targv [i ] # stream描述 c:ip:port 或 l:port
135
+ tlist = [] # 线程列表,最终存放两个线程对象
136
+ targv = [sys .argv [1 ], sys .argv [2 ] ]
137
+ for i in [0 , 1 ]:
138
+ s = targv [i ] # stream描述 c:ip:port 或 l:port
138
139
sl = s .split (':' )
139
- if len (sl ) == 2 and (sl [0 ] == 'l' or sl [0 ] == 'L' ): # l:port
140
- t = threading .Thread (target = _server ,args = (int (sl [1 ]),i ))
140
+ if len (sl ) == 2 and (sl [0 ] == 'l' or sl [0 ] == 'L' ): # l:port
141
+ t = threading .Thread (target = _server , args = (int (sl [1 ]), i ))
141
142
tlist .append (t )
142
- elif len (sl ) == 3 and (sl [0 ] == 'c' or sl [0 ] == 'C' ): # c:host:port
143
- t = threading .Thread (target = _connect ,args = (sl [1 ],int (sl [2 ]),i ))
143
+ elif len (sl ) == 3 and (sl [0 ] == 'c' or sl [0 ] == 'C' ): # c:host:port
144
+ t = threading .Thread (target = _connect , args = (sl [1 ], int (sl [2 ]), i ))
144
145
tlist .append (t )
145
146
else :
146
147
_usage ()
@@ -151,5 +152,3 @@ def _connect(host,port,num):
151
152
for t in tlist :
152
153
t .join ()
153
154
sys .exit (0 )
154
- #EOF
155
-
0 commit comments