Online games?


24-08-2007 05:53:25

I don't know anything about socket programming or anything like that. However, a game I'm planning would be primarily multiplayer. Now the game isn't "active" online, it wouldn't even require a fast connection. Basically, I just want it so that both players are on the same timer for each round if there's a timer (if not, they just need to know when eachother is ready), then it sends some data (which I'll probably use XML for) and goes to the next round.

Is this possible to do in Python without any previous network programming experience?


24-08-2007 08:04:41

So you mean your game doesn´t need synchronization. There are a few possibilities for you. The first question is can you use a server or do the players have to be connected directly? If you have the ability to use a server there are two simple ways:
1. Learn PHP, make a PHP script and then you only need one line to use it from Python.
2. Python has some internet/network functionality included. Look into the Python Documentation. The things you want should be possible with it.

If you can´t use a server there are two possibilities:
1. Again the things included in Python. It should be possible to do a direct connection with python, but I think that is much harder. I wouldn´t try this.
2. I think PythonOgre will have Raknet included in one of the next releases. This library can do all the Network/Internet functionality. I am waiting for this. 8)


24-08-2007 22:28:37

I know PHP already :) I wasn't aware you could connect it to Python! I have webspace with PHP access, but not my own server. Would that be enough? And how would I do it?


25-08-2007 01:32:57

Personally I'd start with the Socket and SocketServer modules as there are very easy to use and you can always replace them at a later date if you need something 'fancier'

Here's some server code:
VERY simple and crude Python server

Receives data in a none state ful fashion -- loops as an echo server for a while
and after 10 loops sends a 'Done' to the client and tells the server to stop


import socket as s
import SocketServer as ss

class Handler ( ss.BaseRequestHandler ):
def handle ( self ):
datain = self.request[0] # get the input string
s=datain.split() ## Assume input data is <count><space><rest of info>
count = int ( s[0] )

sock = self.request[1] ## this is the incomming socket

## because this is a UDP demo we need to connect to send info back
## I suspect this isn't needed if we use TCP server
sock.connect ( self.client_address )

if count > 10: # we are only looping 10 times
sock.send ("Done")
self.server.closenow = True # tell the server we are done..
sock.send ( "Back at you: " + datain) ## send data back

class Server(ss.ThreadingMixIn, ss.UDPServer): pass

if __name__ == "__main__":
# we create a server on the local machine listening to port 4444..
# could also use "localhost" instead of gethostname()
(family, socktype, proto, canonname, sockaddr) = s.getaddrinfo ( s.gethostname(), 4444 )[0]
myServer = Server ( sockaddr, Handler )
myServer.closenow = False

while not myServer.closenow:

And the client:
VERY simple and crude client code.
Assumes there is a server that will echo the sent data and when it's time to close
the server will send "Done"


import socket as s

if __name__ == "__main__":
# we need to send (and recieve) data on the local machine using port 4444
(family, socktype, proto, canonname, sockaddr) = s.getaddrinfo ( s.gethostname(), 4444 )[0]

socket = s.socket( s.AF_INET, s.SOCK_DGRAM ) # create the client socket, note I'm testing with UDP traffic
socket.connect ( sockaddr )
loop = True
count = 0
while loop:
socket.send ( str(count) + " hello out there")
incomming = socket.recv ( 256 )
print "Received:", incomming
if incomming == "Done": ## special -- it's up to the server to decided when we are done...
loop = False
count += 1



25-08-2007 10:04:25

If you want to connnect it with PHP you just simply have to call a URL. I think you need 3 lines in Python for it. You give your data via Its called cookie. Of course what andy says is more professional, but I would prefer PHP, beccause I wouldn´t have to learn any new.
I´ll give you the three lines some times, but first I will work on the nxogre tuts. :D


22-09-2007 10:16:07

Sockets are really quite simple, and functional - everything uses them at some level. For this kind of project you don't even need to deal with the tough logistics of sockets, since you don't need the speed. You could use a web server and use python's http modules for it, but I think sockets would be easiest to make work for this. Just read up on them, and if you need help, there are millions of tutorials for this kind of thing.

Network programming is easy, what's hard is when you need it to deal with lag. Also, networking is sort of like doubling the complexity of a game simulation, but that only starts showing up when the game simulation is already somewhat complex. You shouldn't have too much trouble with this, and it is a good project to learn network programming with.

I wouldn't use php, it seems like extra hassle to separate coding between multiple languages. If you have a data structure, such as a game piece, you can use the same code on the client and the server, which makes things much simpler.


22-09-2007 15:16:18

You could also try Pyro, which lets you deal remote python objects pretty seamlessly.

Check out this really simple example: (code and test from here)
Minimal example code: the Server

import Pyro.core
import Pyro.naming

class JokeGen(Pyro.core.ObjBase):
def joke(self, name):
return "Sorry "+name+", I don't know any jokes."


Minimal example code: the Client
import Pyro.core

# finds object automatically if you're running the Name Server.
jokes = Pyro.core.getProxyForURI("PYRONAME://jokegen")

print jokes.joke("Irmen")


25-09-2007 10:42:35

And Plib (sound and network support only) is part of the Python-Ogre SVN and will be in the 1.1 release so yet another networking option..