Server program w/Plugins? Anyone have suggestions for improvements?
Benjamin Schollnick
junkster at nospam.rochester.rr.com
Mon Mar 13 19:27:22 EST 2000
Folks,
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
this....:
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
more.
- 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,
"+name+".return_data)"
exec (import_details)
imported_data.append (tempdata)
try:
import_details = name+".startup()"
exec (import_details)
except:
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]() )
sock_list[len(sock_list)-1].listen(5)
sock_list[len(sock_list)-1].setblocking(0)
while 1:
for count in range(0, len(sock_list) ):
temp_connection = []
temp_connection.append(sock_list[count])
ready_read, ready_write, in_error =
select.select(temp_connection, temp_connection, temp_connection, 1)
for conn in ready_read:
goodconnection = (conn, addr) = conn.accept()
conn.send (data[count][1]( conn ) )
conn.close()
#time.sleep(1)
--------------------------------------
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__":
pass
====================================================
(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 127.0.0.1
Chairman William Kennard: bkennard at fcc.gov
Commissioner Susan Ness: sness at fcc.gov
Commissioner Harold Furchtgott-Roth: hfurchtg at fcc.gov
Commissioner Michael Powell: mpowell at fcc.gov
Commissioner Gloria Tristani: gtristan at fcc.gov
consumerline at ftc.gov
fccinfo at fcc.gov
ssegal at fcc.gov
More information about the Python-list
mailing list