Server program w/Plugins? Anyone have suggestions for improvements?

Benjamin Schollnick junkster at
Tue Mar 14 01:27:22 CET 2000


	In my quest for the "perfect" server design.... I came up with a 
plugin system, that
	works fine.... But I'm wondering if there is a easier way to do 

	Plugin's are structured like this, with filename of PORTx.PY, where X
is 0..Max port number 
	(I.e. port1, port37, etc)

	Any suggestions & improvements are welcome.  This server system will 
probably join the
	"Auto Update System", and "Time Server / client" on Source forge once
I polish it up some

			- Benjamin

def startup ():
	print "starting port 001"

def port_number():
	return 1

def return_data( connection ):
	return 'port1'

#print "port 001 imported"

	There server, reads the local directory, and imports the files.... 
Builds a table with the 
	address's of the functions.... 

import os
import select
from   socket import *
import string
import struct
import sys
import time

def     run_command (text):
        exec (text)

#       Find the Port*.py files in the current directory
def     define_port_packages ():
        importthese = []
        filelist = os.listdir ('./')
        for name in filelist:
                if (string.upper(name[0:4]) == 'PORT') and 
(string.upper(os.path.splitext(name)[1]) == '.PY'):
                        importthese.append (os.path.splitext(name)[0])
        return importthese

#       Import the Port*.py files
def     import_packages ( importthese ):
        import_command = "import "
        imported_data = []        
        for name in importthese:
                exec (import_command + name)
                import_details = "tempdata = ("+name + ".port_number, 
                exec (import_details)
                imported_data.append (tempdata)
                        import_details = name+".startup()"
                        exec (import_details)
                        print name+" does not have a startup function"

        return imported_data

files = define_port_packages()
data  = import_packages ( files )

#       Bind the port(s)
sock_list = []
for count in range(0, len(data)):
        sock_list.append (socket (AF_INET, SOCK_STREAM))
        sock_list[len(sock_list)-1].bind ('', data[count][0]() )

while 1:
        for count in range(0, len(sock_list) ):
                temp_connection = []
                ready_read, ready_write, in_error =, temp_connection, temp_connection, 1)
                for conn in ready_read:
                        goodconnection = (conn, addr) = conn.accept()
                        conn.send (data[count][1]( conn ) )

Bonus, here's the Time Server rewritten as a plugin.

import  configf
import  struct
import  sys
import  time
import  tji_logfile

def port_number():
	port_id_number  = 37
	return port_id_number

def port_name():
	name_of_port    = "Date/Time"
	return name_of_port

def timeserver_calculation():
	total = configf.return_epoch()+ time.time()
	return total

def return_data( connection ):
	ft      = timeserver_calculation()
	lt      = long(ft)
	packed  = struct.pack("!L", lt)
	return packed

if __name__ == "__main__":

          (Remove "NoSpam" to Email me)
Please feel free to copy any and or all of this sig.
A little something for spam bots:

root at localhost postmaster at localhost admin at localhost
abuse at localhost postmaster at

Chairman William Kennard: bkennard at 
Commissioner Susan Ness: sness at
Commissioner Harold Furchtgott-Roth: hfurchtg at
Commissioner Michael Powell: mpowell at
Commissioner Gloria Tristani: gtristan at
consumerline at
fccinfo at
ssegal at

More information about the Python-list mailing list