Simplify netlib and improve API.

This commit is contained in:
Aldo Cortesi 2012-06-16 13:53:24 +12:00
parent 4e53f1ee90
commit 18a03c063e
2 changed files with 21 additions and 20 deletions

View File

@ -1,4 +1,4 @@
import select, socket, threading import select, socket, threading, traceback
from OpenSSL import SSL from OpenSSL import SSL
@ -124,9 +124,6 @@ class TCPServer:
self.server_address = self.socket.getsockname() self.server_address = self.socket.getsockname()
self.socket.listen(self.request_queue_size) self.socket.listen(self.request_queue_size)
def fileno(self):
return self.socket.fileno()
def request_thread(self, request, client_address): def request_thread(self, request, client_address):
try: try:
self.handle_connection(request, client_address) self.handle_connection(request, client_address)
@ -139,8 +136,8 @@ class TCPServer:
self.__is_shut_down.clear() self.__is_shut_down.clear()
try: try:
while not self.__shutdown_request: while not self.__shutdown_request:
r, w, e = select.select([self], [], [], poll_interval) r, w, e = select.select([self.socket], [], [], poll_interval)
if self in r: if self.socket in r:
try: try:
request, client_address = self.socket.accept() request, client_address = self.socket.accept()
except socket.error: except socket.error:
@ -160,14 +157,25 @@ class TCPServer:
def shutdown(self): def shutdown(self):
self.__shutdown_request = True self.__shutdown_request = True
self.__is_shut_down.wait() self.__is_shut_down.wait()
self.handle_shutdown()
def handle_error(self, request, client_address): def handle_error(self, request, client_address):
print '-'*40 """
print 'Exception happened during processing of request from', Called when handle_connection raises an exception.
print client_address """
import traceback print >> sys.stderr, '-'*40
traceback.print_exc() # XXX But this goes to stderr! print >> sys.stderr, "Error processing of request from %s"%client_address
print '-'*40 traceback.print_exc()
print >> sys.stderr, '-'*40
def handle_connection(self, request, client_address): def handle_connection(self, request, client_address):
"""
Called after client connection.
"""
raise NotImplementedError raise NotImplementedError
def handle_shutdown(self):
"""
Called after server shutdown.
"""
pass

View File

@ -12,12 +12,6 @@
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
A simple proxy server implementation, which always reads all of a server
response into memory, performs some transformation, and then writes it back
to the client.
"""
import sys, os, string, socket, time import sys, os, string, socket, time
import shutil, tempfile, threading import shutil, tempfile, threading
import optparse, SocketServer import optparse, SocketServer
@ -535,8 +529,7 @@ class ProxyServer(netlib.TCPServer):
def handle_connection(self, request, client_address): def handle_connection(self, request, client_address):
ProxyHandler(self.config, request, client_address, self, self.masterq) ProxyHandler(self.config, request, client_address, self, self.masterq)
def shutdown(self): def handle_shutdown(self):
netlib.TCPServer.shutdown(self)
try: try:
shutil.rmtree(self.certdir) shutil.rmtree(self.certdir)
except OSError: except OSError: