How to use Sockets with wxPython?

Josiah Carlson jcarlson at nospam.uci.edu
Mon Mar 15 18:10:14 EST 2004


> I am having this problem:
> I use Python for my teaching and right now we are trying to implement some
> sort of very simple graphical game the pupils can play across the
> Internet. For this, we use wxPython.
> 
> Now it has turned out that wxPython does not implement wxWidget sockets,
> so we have to use Python sockets instead. Implementing a separate thread
> for this would be too complicated and I am going to use the wxIdle event
> to do the socket processing, as the amount of data sent from one player to
> the other is very small.
> 
> Could somebody show me, how to use Pythin socket in "polling mode" and
> how to achieve this? I am not expecting a complete program, an outline of
> the logic woul suffice.

I would suggest you stay away from wxIdle events, if only because they 
only occur under certain circumstances (in Windows, they seem to only 
occur when the mouse is moved over the window).

Start with asyncore, and get everything working properly with that.

When it is working properly, do something like the following for your GUI:

import asyncore
import yourasyncorelibrary

         #in the __init__ method of your main window class
         #set up the server/clients/whatever

         #using the new namespace
         self.timer = wx.Timer(self, wx.NewId())
         EVT_TIMER(self, self.timer.GetId(), self.SocketPoller)
         self.timer.Start(10, wx.TIMER_CONTINUOUS)

     def SocketPoller(self, event):
         asyncore.poll()

If you properly designed your server and clients, the above should just 
work.  If you are willing to post information about your architecture or 
protocol, I may be able to help you more.

  - Josiah



More information about the Python-list mailing list