multi command session through a socket

Nomad nomad*** at ***freemail.absa.co.za
Fri Oct 12 07:19:54 EDT 2001


Hi,

I've been playing with sockets and serversockets in Python (2.1 win32)
for the last few days.  I've been trying to adapt the code from
'BaseHTTPServer.py' in the standard library, but no matter what I try,
the server side always drops the connection after only one command,
whereas I'd like to be able to run several commands (like telnetting
into a pop3 session) and finishing with a QUIT command (or hopfully
later a timeout, but that's another question entirely).

The only code I actually have running in my BaseServer right now is:

# start sample code:::
    def handle(self):
	# Welcome the user
	welcome = '+OK Welcome, ' +  self.server_version + ' ' + \
self.sys_version + ' ready.\r\n'

	self.wfile.write(welcome)
	self.wfile.flush()
	
	self.quit = 1
	while self.quit == 1: # <---- what is wrong here
		self.raw_requestline = self.rfile.readline()
		if not self.parse_request():
			return
		
    def parse_request(self):		
	requestline = self.raw_requestline
	if requestline.find(' ') == -1:
		command = requestline
	else:
		try:
			command, requestline = \
requestline.split(None,1)
		except:
			self.quit = 0
			return 0
	if command.upper().strip() == 'HELLO':
		self.wfile.write('+OK Waddaya mean 'ello?\r\n')
		self.wfile.flush()
		self.quit = 1
	if command.upper().strip() == 'QUIT':
		self.quit = 0

# end sample code:::

I was hoping that the code at the "<---- what is wrong here" marker
would loop through until self.quit had been set to 0 (in
self.parse_request) .  However, it seems that the code still only
accepts one command, and then closes the connection.

Any ideas on how to keep the connection up until a QUIT command is
sent?  And beyond that, is there a simple 'timeout' function that
would fit into such a senario?

TIA

-- 
Nomad

Wondering of the vast emptyness of the 'net
in search of something cool.



More information about the Python-list mailing list