On Sun, 21 Aug 2005, John Walton wrote:

> Hello, everyone.  I just began school, and they already assigned us 
> science fair.  Since I'm in 8th grade, I get to do demonstrations for 
> our projects. I'm probably going to demonstrate Python's networking 
> capabilities by writing a simple instant messenger program.  I only have 
> a few problems:
>
> 1. I know squat about Python network Programming
>
> 2. I know nothing about networks
>
> So if any of you know of a good Python Networking Tutorial or a website 
> with lots of information on networks and networking, please reply. 
> Thanks!

There are two sides to this problem. The first is understanding networks 
in general, and the specific application protocols you're interested in. 
When i say 'understanding networks in general', don't panic - i don't mean 
you need to understand everything about how the internet works. In fact, 
you don't really need to understand *anything* about how the internet 
works, you just need to understand the interfaces it exposes to you. And, 
helpfully, that interface is pretty simple: a program can get a connection 
to another program, running on a different machine, which amounts ot a 
pipe for bytes - both ends can write bytes to the pipe when they feel like 
it, and those bytes become available for the other end to read. To open 
one of these connections, you need to know the hostname or IP address of 
the computer at the far end, and something called a 'port number', which 
is basically a way of identifying which program on that machine you want 
to talk to; if you want other programs to be able to open connections to 
your program, you have to pick a port number and ask the system to give 
you any connections that are made to it.

That's pretty much it for the network fundamentals. There is more - 
datagram sockets, looking up IP addresses, doing funky things with sockets 
- but you can forget about that until you've mastered the basics.

What you do need to understand beyond this, though, is about the 
application protocol you're using. The network just gives you a way to 
move streams of bytes; in order to actually do anything useful, you need 
an agreement between the programs at either end of the connection about 
what those bytes mean - that's an application protocol. It's basically a 
file format as applied to a network connection instead of a file. Each 
application protocol is completely different to every other one (well, 
there are a lot of similarities, but they're mostly different), so you'll 
need to read up on the one you want to use (or invent your own!) - the 
documentation is (almost always) in the form of a document unhelpfully 
called a Request For Comments, or RFC; the internet RFCs are published 
here:

http://www.rfc-editor.org/

For example, here's the RFC for HTTP version 1.0:

http://www.rfc-editor.org/rfc/rfc1945.txt

RFCs can be pretty heavy going, but they are *the* definitive 
specifications, so they're worth reading. Once you're used to them, 
they're often easier to read than tutorials, i find.

The second thing is understanding how to do network programming in python. 
There's a well-established API in C for network programming - the socket 
API - which comes from UNIX; python uses a fairly simple translation of 
this as its network API (look in the 'socket' package). The good thing 
about this is that this API is well-understood and well-documented. The 
bad thing is that it's a bit of a mess (compare and contrast to the API in 
Java if you don't believe me). There's detailed documentation for the 
socket module here:

http://docs.python.org/lib/module-socket.html

And a very quick tutorial here:

http://www.amk.ca/python/howto/sockets/sockets.html

What it comes down to, though, is that you can do:

import socket
s = socket.socket()
target = ("www.python.org", 80)
s.connect(target)
s.send("GET / HTTP/1.0\r\n\r\n")
reply = ""
while True:
        line = s.recv(1000)
        if (line != ""):
                reply = reply + line
        else:
                break
s.close()

To create a connection, send some data, and then read some data. In this 
case, the code sends a very simple HTTP request.

A slightly easier way to do this is using socket's makefile method - this 
gives you a file-like representation of the socket, so you can read and 
write data using the familiar file methods.

To accept connections from other machines, do something like this:

ss = socket.socket()
ss.bind(('', 2323))
ss.listen(5)
while True:
        s, addr = ss.accept()
        s.send("Hello!\r\n")
        s.close()

tom

-- 
Eat + Read + Learn = Word
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to