473,699 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.B aseRequestHandl er):
'''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.re cv(bufsize)
name = ""
if ('NAME:' in line):
name = line.split('NAM E:')[1]
self.request.se ndall('hello there %s \n' % name)

def manageServer():
'''Function to manage the running of the server and handling of
servObj = SocketServer.TC PServer(('local host',6099),Gre etingHandler)
print 'Starting the server...'
servObj.serve_f orever()

if __name__ == '__main__':

----- 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(s ocket.AF_INET, socket.SOCK_STR EAM)
servObj.connect ((server, port))
servObj.send('N AME: An Elephant\r\n')
resp = servObj.recv(81 92)
print resp

if __name__ == '__main__':

I think that the problem with the rfile and wfile methods in the
StreamRequestHa ndler 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 StreamRequestHa ndler


-----Original Message-----
From: Frederick Grim [mailto:fgrim at norby.dyndns.or g]
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
misunderstandin g of sockets. I have a class defined like so:

class tcp_listener(So cketServer.Thre adingTCPServer) :
def __init__(self, addr, port):
SocketServer.Th readingTCPServe r.__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
def __req_handle(se lf, req):
""" Do stuff with req and return a response afterwards """
return response

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

self.wfile.writ e(self.__req_ha ndle(request))

The client end looks almost identical to the example in the python docs.
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.
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
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.

Jul 18 '05 #1
0 2933

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

Similar topics

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 = self.rfile.read(1024)
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 the same thing the socketserver module, it's ok for the client, but I don't succeed in making work the server :-(( Here is the client (which works)
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 functionalty but connecting to a server. I have no idea about programming with python so every hint is a present for me :) ------------------------------------ import SocketServer import os
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 like so: class tcp_listener(SocketServer.ThreadingTCPServer): def __init__(self, addr, port): SocketServer.ThreadingTCPServer.__init__(self, \ (addr, port), Daemon.request_handler)
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 handle_request loop instead of serve_forever, it still seems difficult: class myserver(ThreadingMixIn, TCPServer): pass server = myserver(...) server.shutdown = False while not server.shutdown: server.handle_request()
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 again, but if I let it run out of data, and exit via handle_error as can be seen below, I will get a socket.error: (98, 'Address already in use') when I try to restart the server. (Unless I wait a minute or so, or use another port.) I feel like I'm...
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 the details of my code (on request available, though), here is what I do : I have a TCPServer and BaseRequestHandler .
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? import SocketServer import logging as l l.basicConfig(level=l.DEBUG,
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) self.socket.bind(self.server_address) class LogsDumpHandler(SocketServer.DatagramRequestHandler): def handle(self):
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.