1 # This file is part of python-rwhoisd
3 # Copyright (C) 2003, David E. Blacka
5 # $Id: RwhoisServer.py,v 1.3 2003/04/28 16:45:11 davidb Exp $
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 import sys, socket, SocketServer
24 import config, Session
25 import QueryParser, QueryProcessor, DirectiveProcessor, Rwhois
28 # server-wide variables
30 query_processor = None
31 directive_processor = None
33 class RwhoisTCPServer(SocketServer.ThreadingTCPServer):
34 def __init__(self, server_address, RequestHandlerClass):
35 self.allow_reuse_address = True
36 SocketServer.TCPServer.__init__(self, server_address,
39 def verify_request(self, request, client_address):
40 # implement access control here
43 class RwhoisHandler(SocketServer.StreamRequestHandler):
46 """Read a line of input from the client."""
47 # a simple way of doing this
48 # return self.rfile.readline()
50 data = self.request.recv(1024)
51 if not data: return None
53 lines = data.splitlines(True)
55 # ugh. this totally defeats any pipelining, not that rwhois
56 # clients should be doing that.
57 if len(lines) > 1 and config.verbose:
58 print "%s discarding additional input lines: %r" \
59 % (self.client_address, lines)
64 self.quit_flag = False
67 self.wfile.write(config.banner_string);
68 self.wfile.write("\r\n");
71 session = Session.Context()
72 session.rfile = self.rfile
73 session.wfile = self.wfile
76 print "%s accepted connection" % (self.client_address,)
80 line = self.readline()
84 # we can skip blank lines.
88 if line.startswith("-"):
89 self.handle_directive(session, line)
91 self.handle_query(session, line)
92 if not session.holdconnect:
97 # check to see if we were asked to quit
98 if self.quit_flag: break
101 print "%s disconnected" % (self.client_address,)
103 def handle_directive(self, session, line):
105 print "%s directive %s" % (self.client_address, line)
106 if (line.startswith("-quit")):
107 self.quit_flag = True
108 self.wfile.write(Rwhois.ok())
110 directive_processor.process_directive(session, line)
112 def handle_query(self, session, line):
114 print "%s query %s" % (self.client_address, line)
115 query_processor.process_query(session, line)
120 usage: %s [-v] schema_file data_file [data_file ...]
121 -v: verbose """ % pname
129 opts, argv = getopt.getopt(argv[1:], 'v')
132 config.verbose = True
134 if len(argv) < 2: usage(pname)
135 schema_file = argv[0]
136 data_files = argv[1:]
141 db.init_schema(schema_file)
142 for df in data_files:
148 global query_processor, directive_processor
150 query_processor = QueryProcessor.QueryProcessor(db)
151 directive_processor = DirectiveProcessor.DirectiveProcessor(db)
154 # initialize the TCP server
155 server = RwhoisTCPServer((config.server_address, config.port),
158 # and handle incoming connections
160 if not config.server_address:
161 print "listening on port %d" % config.port
163 print "listening on %s port %d" % \
164 (config.server_address, config.port)
165 server.serve_forever()
169 if __name__ == "__main__":