From 65444c5b066cd1a64f3b4e8fcb67f59cbac204b4 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 30 Dec 2012 14:01:41 +0800 Subject: [PATCH] use sendall() --- README.md | 6 +++++- local.py | 10 ++++------ server.py | 9 +++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index b93107f..9472e79 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,12 @@ You can use args to override settings from `config.json`. You may want to install gevent for better performance. + $ apt-get install python-gevent + +Or: + $ sudo easy_install gevent - + troubleshooting --------------- diff --git a/local.py b/local.py index 8a5e507..d0f9452 100755 --- a/local.py +++ b/local.py @@ -52,7 +52,7 @@ def get_table(key): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): - pass + allow_reuse_address = True class Socks5Server(SocketServer.StreamRequestHandler): @@ -65,14 +65,13 @@ class Socks5Server(SocketServer.StreamRequestHandler): data = sock.recv(4096) if data <= 0: break - if remote.send(self.encrypt(data)) <= 0: - break + remote.sendall(self.encrypt(data)) + if remote in r: data = remote.recv(4096) if data <= 0: break - if sock.send(self.decrypt(data)) <= 0: - break + sock.sendall(self.decrypt(data)) finally: sock.close() remote.close() @@ -162,7 +161,6 @@ if __name__ == '__main__': decrypt_table = string.maketrans(encrypt_table, string.maketrans('', '')) try: server = ThreadingTCPServer(('', PORT), Socks5Server) - server.allow_reuse_address = True logging.info("starting server at port %d ..." % PORT) server.serve_forever() except socket.error, e: diff --git a/server.py b/server.py index 3558e22..552599e 100755 --- a/server.py +++ b/server.py @@ -52,7 +52,7 @@ def get_table(key): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): - pass + allow_reuse_address = True class Socks5Server(SocketServer.StreamRequestHandler): @@ -65,14 +65,12 @@ class Socks5Server(SocketServer.StreamRequestHandler): data = sock.recv(4096) if data <= 0: break - if remote.send(self.decrypt(data)) <= 0: - break + remote.sendall(self.decrypt(data)) if remote in r: data = remote.recv(4096) if data <= 0: break - if sock.send(self.encrypt(data)) <= 0: - break + sock.send(self.encrypt(data)) finally: sock.close() remote.close() @@ -137,7 +135,6 @@ if __name__ == '__main__': ThreadingTCPServer.address_family = socket.AF_INET6 try: server = ThreadingTCPServer(('', PORT), Socks5Server) - server.allow_reuse_address = True logging.info("starting server at port %d ..." % PORT) server.serve_forever() except socket.error, e: