473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SocketServer class examples


Hello Fred,
I just ran across your question. I think that the following
code will work:

----- SERVER CODE ------
import SocketServer
import time
class GreetingHandler(SocketServer.BaseRequestHandler):
'''Class to handle sending a greeting to a client
'''
def handle(self):
'''Method to actually handle the greeting
'''
print 'handling the request'
line = self.request.recv(bufsize)
name = ""
if ('NAME:' in line):
name = line.split('NAME:')[1]
self.request.sendall('hello there %s \n' % name)

def manageServer():
'''Function to manage the running of the server and handling of
requests
'''
servObj = SocketServer.TCPServer(('localhost',6099),Greeting Handler)
print 'Starting the server...'
servObj.serve_forever()

if __name__ == '__main__':
manageServer()

----- CLIENT CODE -----
#!/usr/bin/env python
import socket
def manageClient():
'''Function to manage the sending of request to the server
'''
server = 'localhost'
port = 6099
servObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servObj.connect((server, port))
servObj.send('NAME: An Elephant\r\n')
resp = servObj.recv(8192)
print resp
servObj.close()

if __name__ == '__main__':
manageClient()

I think that the problem with the rfile and wfile methods in the
StreamRequestHandler class is that it's trying to send on the
same socket as it's reading data on so they are blocking each
other. I think that this is a problem in the StreamRequestHandler
class.

adil

-----Original Message-----
From: Frederick Grim [mailto:fgrim at norby.dyndns.org]
Sent: 18 July 2004 19:34
To: python-list at python.org
Subject: SocketServer class examples

Howdy group,
So I am in the middle of using the socketserver class from the std
library and have run into a problem that probably reveals my
misunderstanding of sockets. I have a class defined like so:

class tcp_listener(SocketServer.ThreadingTCPServer):
def __init__(self, addr, port):
SocketServer.ThreadingTCPServer.__init__(self, \
(addr, port), Daemon.request_handler)

""" Yes I realize the above is silly and redundant """

And a request handler in Daemon that has a handle function that works like
so:
def __req_handle(self, req):
""" Do stuff with req and return a response afterwards """
return response

def handle(self);
while True:
input = self.rfile.readline()
request = input
while input and not re.search('EOF$', input):
input = self.rfile.readline()
request += input

self.wfile.write(self.__req_handle(request))

The client end looks almost identical to the example in the python docs.
So
the problem here is that this code doesn't work. Using tcpdump I can tell
that the client is sending to the server properly but the server is never
responding. Or when it tries to respond it gets stuck in the write.
What's
going on here. I can't seem to find a single example of how to use this
class on the client and server side and I don't want to use twisted
(because
I should understand how this works instead of relying on canned software).
Does anyone know where I can find an example of a functioning SocketServer
and client? Google seems to help nought here.
Thanks,
Fred


Jul 18 '05 #1
0 2908

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
So I'm new to this python stuff - and this has me stumped # server import SocketServer PORT = 8037 class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): self.input...
3
by: Olivier Hoarau | last post by:
Hello, I have build a client/server application with the socket module. The server mades UDP broadcasting and the client only reads UDP broadcast messages. All work fine. Now I want to use for...
3
by: Ergin Aytac | last post by:
I'm trying to run a script written in python and have some socket connection problems. I cutted the origin script (more than 1000 lines) so it is only the part of the connection and there is no...
0
by: Frederick Grim | last post by:
Howdy group, So I am in the middle of using the socketserver class from the std library and have run into a problem that probably reveals my misunderstanding of sockets. I have a class defined...
12
by: Paul Rubin | last post by:
Let's say you have a SocketServer with the threading mix-in and you run serve_forever on it. How can you shut it down, or rather, how can it even shut itself down? Even if you use a...
3
by: Magnus Lycka | last post by:
I have a socket server like below which I want to exit when it's out of data. If I interrupt the client, I'll get a broken pipe on the server side, and after a Ctrl-C, I can restart the server...
4
by: google | last post by:
Dear newsgroup, I give up, I must be overseeing something terribly trivial, but I can't get a simple (Java) applet to react to incoming (python) SocketServer messages. Without boring you with...
0
by: Tomi Hautakoski | last post by:
Hello, I'm a Python newbie trying to figure out how to use SocketServer with IPv6. I would like to set up a TCPServer working like below but how to tell SocketServer I need to use AF_INET6? ...
4
by: id.engg | last post by:
logFileName = 'log.txt' logfile = open(logFileName, "a") class MyUDPServer(SocketServer.UDPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8388608)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.