views:

552

answers:

6

The question: How do I create a python application that can connect and send packets over the internet to another computer running the same application? Is there any existing code/library I could use?

The background: I am pretty new to programming (HS senior). I've created a lot of simple things in python but I've recently decided to start on a bigger project. I'm considering creating a Magic: the Gathering booster draft simulator, but I'm not sure if it is feasible given my skill set so I'm asking around before I get started. The application would need to send data between computers about which cards are being picked/passed.

Thanks!

+6  A: 

Twisted is a python event-driven networking engine licensed under MIT. Means that a single machine can communicate with one or more other machines, while doing other things between data being received and sent, all asynchronously, and running a in a single thread/process.

It supports many protocols out of the box, so you can just as well using an existing one. That's better because you get support for the protocol from 3rd party software (i.e. using HTTP for communication means middleware software that uses HTTP will be compatible: proxies etc.)

It also makes easy to create your own communication protocol, if that's what you want.

The documentation is filled with examples.

nosklo
+1  A: 

A great place to start looking is the Python Standard Library. In particular there are a few sections that will be relevant:

Since you mentioned that you have no experience with this, I'd suggest starting with a HTTP based implementation. The HTTP protocol is fairly simple and easy to work with. In addition, there are nice frameworks to support this operation, such as webpy for the server and HTTPLib from the standard library for the client.

If you really want to dive into networking, then a socket based implementation might be educational. This will teach you the underlying concepts that are used in lots of networking code while resulting in an interface that is similar to a file stream.

Doug
There is a useful howto for sockets: http://docs.python.org/howto/sockets.html.
Bastien Léonard
a strong -1 in recomending low-level sockets to a beginner. In my experience with teaching, teach a high-level framework and then show the underlining implementation makes much more sense than teaching how to do it wrong.
nosklo
I suppose it depends on what the student is looking to get out of it and what level they are really at. From my pov, if I wanted to learn about network communication and somehow didn't ever learn about sockets, I'd be upset. After the initial connection is formed, using sockets is only slightly more difficult than reading/writing a file. The benefit is that the student gets to think about how they want to represent the data.
Doug
I edited the post to suggest HTTP (with a framework), but left in the note about sockets for the curious.
Doug
@Doug: Sockets are really hard to get right. It is *A LOT* more difficult than reading a file. Even if the student wants to learn low-level, she should first learn high-level to get the basics and then study the underlining implementation. Or do you advise learning assembly before python? For your second point, the student will have to think about how to represent the data anyway, and I don't see how learning about sockets will change that in any means.
nosklo
@nosklo There are bottom up and top down approaches to learning. I think it is worth pointing out the bottom up approach for those who are interested. As to the data, surely you must have a "serialized" version for socket communication compared to a framework that might handle this detail for you. That is all that I was pointing out.
Doug
@Doug: And I don't see how serializing yourself the data might help you get the work done - seriously - not using something already written will never get anything done right, you'll only write crappy code, with bugs, security issues, etc. People write serializers, but those people are *not* newbies, to write a serializer you must *AT LEAST* study some of the already existing implementations. The same goes to network code/sockets.
nosklo
@nosklo My response is not directed toward anyone writing production code, but instead is directed toward those who wish to learn something. Obviously there are different methods to learning the same concepts. I concede that starting with the socket interface is not the quickest way to get working code and is also probably not the best choice for a robust/stable system. However, I stand by my statement that learning how to use the socket interface is valuable knowledge.
Doug
@nosklo Also, when I speak about "serializing" the data, I mean this in the most basic way, such as using a string as the message and simply sending an integer with the size of the string followed by the string itself. This is simple yet enough to build a prototype/example system for the learner's sake.
Doug
@Doug: Those who wish to learn will want to learn something useful instead! Some time ago learning how to put a pixel in screen in asm was useful but nowadays you only use higher-level approaches. The same goes to networking code - everyone wanting to learn and actually *use* the knowledge should look for the higher level stuff, until ready to write low level. Then, studying existing implementations is essential. I really don't think learning low-level socket API is valuable knowledge for a newbie. It's possible to learn network basics on the higher level API so low-level gives no advantage.
nosklo
+4  A: 

The standard library includes SocketServer (documented here), which might do what you want.

However I wonder if a better solution might be to use a message queue. Lots of good implementations already exist, including Python interfaces. I've used RabbitMQ before. The idea is that the computers both subscribe to the queue, and can either post or listen for events.

Daniel Roseman
+1: use a implementation of any library that can help you
nosklo
+1  A: 

Communication will take place with sockets, one way or another. Just a question of whether you use existing higher-level libraries, or roll your own.

If you're doing this as a learning experience, probably want to start as low-level as you can, to see the real nuts and bolts. Which means you probably want to start with a SocketServer, using a TCP connection (TCP is basically guaranteed delivery of data; UDP is not).

Google for some simple example code. Setting one up is very easy. But you will have to define all the details of your communications protocol: which end sends when and what, which end listens and when, what exactly the listener will expect, does it reply to confirm receipt, etc.

John Pirie
-1: I strongly disagree that seeing TCP sockets nuts and bolts will help anything. Using socket directly will just make you learn how to *NOT* do your code. Just learn high level instead, and then study whichever library you chose.
nosklo
I do very little programming in C, but I'm very glad I know it, because it gives me a greater depth of understanding as to what higher level languages are actually doing behind the scenes with memory and complex data types. I think the same is true of socket programming: one would almost never go to the TCP level for a production application, but some exercises at that level bring a greater understanding of what the higher-level frameworks are doing.
John Pirie
@John Pirie: Yeah, but do you think a newbie should learn C (or assembly, for that matter) *first*? I don't think so. I think the natural tendency is to learn high-level first, and then go down as much as needed to understand the low level concepts later, after general concepts are well-learned. My point is that the framework should come first.
nosklo
A: 

Also, check out Pyro (Python remoting objects). Se this answer for more details.

Pyro basically allows you to publish Python object instances as services that can be called remotely. Pyro is probably the easiest way to implement Python-to-python process communication.

codeape
+1  A: 

It's also worth looking at Kamaelia for this sort of thing - it's original usecase was network systems, and makes building such things relatively intuitive. Some links: Overview of basic TCP system, Simple Chat server, Building a layered protocol, walk-through of how to evolve new components. Other extreme: P2P radio system: source, peer.

If it makes any difference, we've tested whether the system is accessible to novices through involvement in google summer of code 3 years in a row, actively taking on both experienced and inexperienced developers. All of them managed to build useful systems.

Essentially, if you've ever played with unix pipelines the ideas should be familiar.

Caveat: I wrote major chunks of Kamaelia :-)

If you want to learn how to do these things though, playing with a few different approaches makes sense, and you should definitely check out Twisted (the standard answer to this question), Pyro & the standard library tools. Each has a different approach, and learning them will definitely benefit you!

However, like nosklo, I would recommend against using the socket library directly and use a library instead - simply because it is much much harder to get sockets programming correct than people tend to realise.

Michael Sparks