[ Curiosity,Experimentation ]

Random stuff from the parallel universe of Ones and Zeroes

An Asynchronous Socket Class in Python

Posted by appusajeev on November 23, 2009

The default socket module can be a pain certain times,especially when you are working with asynchronous/event driven domain.Handling asynchronous communication can be a bit messy in the native python environment .The accept() and recv() methods would only relinquish control after a connection has been accepted or data has arrived. This would sometimes choke the flow of control in the program..
So i thought of creating a socket class utilizing threads internally to handle asynchronous communication and this is what i have finally arrived at.
The Winsock control in VB provides a clean yet powerful interface to handle asynchronous socket communication.I have tried to provide an interface similar to the Winsock control. I have also incorporated callback mechanism by which a function could be invoked upon data arrival.This avoids the need to poll the socket every time. As of now i have just provided the methods working upon TCP,UDP is not yet supported though adding UDP support is trivial ! 🙂

Download Source

An Asynchronous Socket Class in Python Source

An Asynchronous Socket Class in Python


3 Responses to “An Asynchronous Socket Class in Python”

  1. Rahul U said

    nice work buddy

  2. ossi said

    what means the func parameter

    • appusajeev said

      Inside the class, func is a variable that hold the address of a function that will be called when data arrives across the socket. Its like a callback function, like dataArrival event in VB6. That function may then read the data from the socket. Hope you got it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: