creating simple threaded socket server

Pete J Shinners pete at visionart.com
Thu Apr 20 02:11:54 CEST 2000


still new to python, and this is my first " > hello,world"
project. eventually the project will be a simplish chat-type
server with some extra enhancements. i know, it's been done
to death, but my learning python is the main goal here, not
breaking any new ground.

i'm starting with a simple server that listens on a socket
and spawns threads to handle incoming connections. it is
working, but i'd strongly like to get some peer review at
this point.

am i headed down the road to destruction? does this look
like a positive start? i am also wondering if there is a
better way to 'unblock' my calls like socket.accepts without
silly(??) workarounds like i have with my unplug() method

anyways, like i said, the server does work, you can telnet
into it and it simple echoes the strings it gets as a list.
sending "Quit" will disconnect your thread, sending "Exit"
will shutdown the server (as best i know how)

i've looked all around for some good examples to follow
but could find none (nothing on vaults or starship) so
i had to do a lot of trailblazing for myself. i also started
with 'SocketServer', but was having difficulty, and decided
to "simplify" things be going lower level? perhaps now that
i have a grasp on things i should move back up to SocketServer?

here's the code, all and any insight is welcome!!
i've been running in interactive mode...
import myserver
myserver.test()

--------------------------------myserver.py---
# simple(??) threaded server in python

from socket import *
import string
import threading
import time

networkactive = 1;




class NetworkListener(threading.Thread):
    def __init__(self, socket, address, manager):
	threading.Thread.__init__(self, None)
	self.socket = socket
	self.address = address
	self.manager = manager
	self.start()
    
    def __del__(self):
	self.manager = None
    
    def run(self):
	global networkactive
	print 'Connected from ', self.address
	self.socket.send('Welcome\n')
	while 1:
	    data = string.strip(self.socket.recv(1024))
	    if not data or data == 'Quit': break
	    if data == 'Exit': networkactive = 0; break
	    self.socket.send(`list(data)`+'\n')
	print 'Disconnecting ', self.address
	self.socket.shutdown(2)
	self.socket.close()





class PortManager(threading.Thread):
    def __init__(self, port):
	threading.Thread.__init__(self, None)
	self.port = port
	self.start()
	
    def run(self):
	global networkactive
	s = socket(AF_INET, SOCK_STREAM)
	s.bind('', self.port)
	s.listen(2)
	while networkactive:
	    sock, addr = s.accept()
	    if networkactive:
		NetworkListener(sock, addr, self)
	s.close()
	print 'SOCKETS DONE'

    def unplug(self):
	"""this will unstick the thread from accept"""
	s = socket(AF_INET, SOCK_STREAM)
	s.connect('', self.port)
	s.close()
	print 'Unsticking ACCEPT call'


 
def test():
    server = PortManager(6016)
    while networkactive:
	time.sleep(2)
	print 'Threads: ', threading.enumerate()
    server.unplug()

----------------------------------------------------



More information about the Python-list mailing list