473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Asyncore

Is there any tutorial and docs with samples how to use asyncore module?
Thanks
Lad

Jul 18 '05 #1
1 2984
ex****@hope.cz wrote:
Is there any tutorial and docs with samples how to use asyncore module?
Thanks
Lad

Well, here's a very simple web server from Chapter 7 of "Python Web
Programming" which might help get you started.

You can see a few more examples if you download the code from the book,
which you can do at

http://pydish.holdenweb.com/pwp/download.htm

--------
import socket
import asyncore
import time

class http_server(asyncore.dispatcher):

def __init__(self, ip, port):
self.ip= ip
self.port = port
self.count = 0
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind((ip, port))
self.listen(5)

def writable(self):
return 0

def handle_read(self):
pass

def readable(self):
return self.accepting

def handle_connect(self):
pass

def handle_accept(self):
try:
conn, addr = self.accept()
except socket.error: # rare Linux error
print "Socket error on server accept()"
return
except TypeError: # rare FreeBSD3 error
print "EWOULDBLOCK exception on server accept()"
return
self.count += 1
handler = http_handler(conn, addr, self, self.count)

def decrement(self):
self.count -= 1

class http_handler(asyncore.dispatcher):

def __init__(self, conn, addr, server, count):
asyncore.dispatcher.__init__(self, sock=conn)
self.addr = addr
self.buffer = ""
self.time = time.time()
self.count = count
self.server = server

def handle_read(self):
rq = self.recv(1024)
self.buffer = """HTTP/1.0 200 OK Canned Response Follows
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Response from server</TITLE>
</HEAD>
<BODY>
<P>This is socket number %d
</BODY>
""" % self.count

def writable(self):
if time.time()-self.time > 10:
return len(self.buffer) > 0
else:
return 0

def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
if len(self.buffer) == 0:
self.close()
self.server.decrement()

server = http_server('', 8080)

asyncore.loop(timeout=4.0)
--------

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #2

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

Similar topics

0
2690
by: Michael Welsh | last post by:
In order to learn sockets in Python I am trying to write a simple group chat server and client. I need a little nudge, please. My question contains some GUI but only as decoration. The root...
5
5925
by: David M. Wilson | last post by:
Hi peeps, I finally got around recently to doing something serious using Python for serving network clients asynchronously, deciding on asyncore as my starting point. After 2 days or so of...
2
2572
by: Freddie | last post by:
Hi, I've been mangling python-irclib into an asyncore class, so it fits in nicely with the rest of my app. I ran into a problem with asyncore.dispatcher_with_send (Python 2.3.4), though. Not...
0
478
by: Neil Benn | last post by:
Hello, I was wondering if someone could provide assistance. I'm writing a thin wrapper around asyncore for compatibility with a common Comms API that I use. class...
3
5247
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
0
1744
by: Tony Meyer | last post by:
Changes in asyncore from 2.3 to 2.4 mean that asyncore.poll() now passes all the sockets in the map to select.select() to be checked for errors, which is probably a good thing. If an error occurs,...
3
3646
by: Jos | last post by:
Hello. I'm using the asyncore and _chat modules to create a network server. I also have, running in a separate thread(s), a "producer" which needs to "push" data onto the network connection(s)....
5
7089
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the...
7
2723
by: billie | last post by:
Hi all. I've just terminated a server application using asyncore / asynchat frameworks. I wrote a test script that performs a lot of connections to the server app and I discovered that asyncore...
0
1090
by: Giampaolo Rodola' | last post by:
Hi, I post this message here in the hope someone using asyncore could review this. Since the thing I miss mostly in asyncore is a system for calling a function after a certain amount of time, I...
0
7242
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
7138
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...
0
7418
jinu1996
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...
0
7508
tracyyun
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...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
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...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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 ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.