network examples / reference?

Looking for a little help on networking. I see in the manual it says to ask here, so here I am. I already tried a search and basically just came up with a bunch of broken links - lots of references to “Network Example” (along with Rubiks example, and one other - all references made from one page), but when I try to go there, I get a 404.

I don’t think what I’m trying to do should be all that difficult. Just want a socket to listen for messages while still having an interactive window. Messages will be to change colors of objects - if I can get a string in, I think I can figure out what to do with it from there…

Any help appreciated!


Just wondering if anyone had some different input on this… I’ve been going through the Panda API and cross-referencing the - looks neat, but I’m not sure it’s quite what I was hoping for. From what I can tell, this is depending on Python at both ends (packaging up a datagram object, sending it over, then unpackaging it). I was hoping for something that could just use a Socket so I can use a different language to communicate with it. I guess I’m looking for a way to open a socket and create a task that will check for data.

Any takers?


If all you want is a low-level socket, try Python’s “socket” module.


I understand you dont want to bother with the packing and unpacking of datagrams, but you could just send a string instead… Is that what you’re looking for?

Yes, I think that would work. I’m just having a hard time figuring out how to have a TCP socket listen and handle events while still allowing Panda to do it’s thing. I’m looking into the Twisted framework, but so far it seems that both Twisted and Panda have some sort of internal event loop that pretty much blocks anything else from happening.

Sorry - I’m kind of new to Python… this is definitely a learning experience for me, so if you can give me any pointers, I’d appreciate it!

This is based on the reference code here:

#Let's start with a simple echo server embedded in code
def server():
        import socket
        conn, addr = s.accept()
        print 'connected by', addr
                data = conn.recv(1024)
                if not data: break

#Now, if you run this with panda, it will cause panda 
#to stop processing. You need to have this running all 
#the time, but you also need panda running all the time

#One of the ways to get around this is to use threading
import thread

#this basically says "call this function inside of a 
#thread, and let it run in parallel with my process"

#finally, here's the host code to test it with.
import socket

s.send('Hello, world')
print 'Recieved', repr(data)

Panda and Twisted both have their own event loops that want to be in control of program flow, and need to be able to run independantly of each other. In the code I’m working on I was able to solve this problem by creating two program threads, one for Panda and one for Twisted, and using two Queue structures to pass messages between the two threads.

In Panda3D I have a task which runs once per frame, which checks the incoming Queue for messages from the server and updates world objects in Panda as needed. Player inputs call event handlers which place messages on the outgoing queue. In Twisted I have a loop which periodically checks the outgoing queue for messages, and sends them to the server as needed. Messages from the server are places on the incoming queue and sent to the Panda task as they happen.

This may not be the most optimal way to do it and still needs a lot of development, but it does solve the problem of running Twisted and Panda simultaneously.

Another, simpler option is not to call run() at all. Panda doesn’t really need to own the main loop.

Instead, simply call Task.step() from time to time, which will run through one iteration of Panda’s loop. In fact, run() is basically just an infinite loop that calls Task.step() repeatedly.

Of course, you have to ensure that Task.step() is called frequently enough to keep your frame rate up.


Hi :slight_smile: zpawlov, I tried your example above, but I get constantly the following error message:

  File "<string>", line 1, in connect
socket.error: (111, 'Connection refused')

What goes wrong there? (Using Python 2.3.5)

He says that he can’t connect to the socket, that I see as well, but I wonder why he can’t connect… however, when running the server part alone, it opens the port and waits for a connection.

But when starting the client part, I get the message mentioned above :frowning:

Where is the error? At my place may be?

Regards, Bigfoot29

Small change to drwr’s post:


doesn’t work. It has to be


Regards, Bigfoot29

That’s what I get for typing in code suggestions off the top of my head! :blush:

Hehe… the “cloaky” Mr Cloak gave me that advice in IRC, just wanted to provide the results here too :smiley:

He seem to know you, David wonders :wink:

Regards, Bigfoot29

im sure this is a really bad way to do it but…

def runEverySecond():
w = World()
l = task.LoopingCall(runEverySecond)
pykeClient = pykeFactory(w)
reactor.connectTCP("localhost", 8007, pykeClient)#(ipaddress,port,factory) 

then in your factory…

class pykeFactory(ClientFactory):
    def __init__(self,wint):
        self.wor = wint

the client factory doesn’t have an init so don’t call it heh
so this way you can access stuff in your world object… like uh a queue:P

Edit: As for a server you could just write that all in twisted:)