Python implements simple TCP proxy server, pythontcp
The example in this article describes how to implement a simple TCP proxy server in python and share it with you for your reference.
The specific implementation code is as follows:
#-*-Coding: UTF-8-*-''' filename: rtcp. py @ desc: Use the socket port forwarding function of python for remote maintenance. If the connection fails, the system will sleep for 36 s, and a maximum of 200 (two hours) @ usage :. /rtcp. py stream1 stream2stream: l: port or c: host: portl: port indicates the local port specified by the listener c: host: port indicates the remote port specified by the listener @ author: watercloud, zd, knownsec team @ web: www.knownsec.com, blog.knownsec.com @ date: 2009-2009-7''' import socketimport sysimport threadingimport timestreams = [None, None] # store the two data streams to be forwarded (both SocketOb J object) debug = 1 # debug status 0 or 1def _ usage (): print 'usage :. /rtcp. py stream1 stream2 \ nstream: l: port or c: host: port 'def _ get_another_stream (num): ''' get another stream object from streams. If it is empty, then wait for '''if num = 0: num = 1 elif num = 1: num = 0 else: raise "ERROR" while True: if streams [num] = 'quit': print ("can't connect to the target, quit now! ") Sys. exit (1) if streams [num]! = None: return streams [num] else: time. sleep (1) def _ xstream (num, s1, s2): ''' the data num of the two streams is the current stream number, which is mainly used for debugging and distinguishing the two loop states. '''Try: while True: # note that the recv function will be blocked until the peer end is completely closed (it takes some time to close it after close, and the fastest way to close it is shutdow) buff = s1.recv (1024) if debug> 0: print num, "recv" if len (buff) = 0: # Close the connection on the peer end and print num cannot be read, "one closed" break s2.sendall (buff) if debug> 0: print num, "sendall" handle T: print num, "one connect closed. "try: s1.shutdown (socket. SHUT_RDWR) s1.close () failed T: pass try: s2.shutdown (socket. SHUT_RDWR) s2.close () failed T: pas S streams [0] = None streams [1] = None print num, "CLOSED" def _ server (port, num): ''' processes service conditions, num is the stream number (0th or 1st) ''' srv = socket. socket (socket. AF_INET, socket. SOCK_STREAM) srv. bind ('0. 0.0.0 ', port) srv. listen (1) while True: conn, addr = srv. accept () print "connected from:", addr streams [num] = conn # Put the local stream object s2 = _ get_another_stream (num) # obtain the stream object _ xstream (num, conn, s2) def _ connect (host, port, n Um): ''' processes the connection. num is the stream number (0th or 1st) @ note: if the connection fails, the system will sleep for 36 s, try up to 200 (that is, two hours) ''' not_connet_time = 0 wait_time = 36 try_cnt = 199 while True: if not_connet_time> try_cnt: streams [num] = 'quit' print ('not connected') return None conn = socket. socket (socket. AF_INET, socket. SOCK_STREAM) try: conn. connect (host, port) failed t Exception, e: print ('can not connect % s: % s! '% (Host, port) not_connet_time + = 1 time. sleep (wait_time) continue print "connected to % s: % I" % (host, port) streams [num] = conn # Put the local stream object s2 = _ get_another_stream (num) # obtain the stream object _ xstream (num, conn, s2) of the other end if _ name _ = '_ main _': if len (sys. argv )! = 3: _ usage () sys. exit (1) tlist = [] # thread list, which stores two thread objects targv = [sys. argv [1], sys. argv [2] for I in [0, 1]: s = targv [I] # stream description c: ip: port or l: port sl = s. split (':') if len (sl) = 2 and (sl [0] = 'L' or sl [0] = 'l'): # l: port t = threading. thread (target = _ server, args = (int (sl [1]), I) tlist. append (t) elif len (sl) = 3 and (sl [0] = 'C' or sl [0] = 'C'): # c: host: port t = threading. thread (target = _ connect, args = (sl [1], int (sl [2]), I) tlist. append (t) else: _ usage () sys. exit (1) for t in tlist: t. start () for t in tlist: t. join () sys. exit (0)
Click here to download the complete instance code.
I hope this article will help you with Python programming.
How to Use the python code to obtain the proxy ip address from the National http Proxy
In general, there are two steps:
1. Obtain the page source code page = urllib. request. urlopen (url). read ()
2. obtain the required IP address and port using regular expressions based on the Rules displayed in the IP address list in the source code. This part needs to be analyzed by the corresponding source code and then written.
3. Get the required information and write it to the file or print it out or how to handle it.
Can python simulate tcp three-way handshake?
Yes