I am designing a Minecraft Classic server written in Python, but I don't know how to send the packets properly. I know how to set up a socket, but the part I don't know is how to send them in the format needed by the protocol. I understand the protocol, just not how to implement it in Python. I don't really have code to post, just the heartbeat, and that's not relevant to my question. I have looked through the source of several servers, but I don't understand them.
As Dan D. mentioned you can use struct.pack to format your data as requested by the protocol. The documentation at http://docs.python.org/library/struct.html has a few examples.
Then you need to take a look at the packet structure at http://www.minecraftwiki.net/wiki/Classic_Server_Protocol#Packet_Protocol which describes in detail what your packets need to look like. According to the packet description you would need a struct.pack format string looking something like "Bbh" for the first 4 bytes.
struct.pack("Bbh", 0, 1, 7)
For a player identification packet with id 1 and protocol version 7. You will have to complete this with the rest of the packet 64 byte string + 1024 byte binary part, both with padding.
EDIT: Just remembered Python might not be native big endian so you might want to throw that into the format string as well.
struct.pack("!Bbh", 0, 1, 7)
You can find examples of packet formats and how to handle them by looking into the source of the myne2 server
https://bitbucket.org/andrewgodwin/myne2/src/236deac8cd2f/myne/core/constants.py
https://bitbucket.org/andrewgodwin/myne2/src/236deac8cd2f/myne/core/packeter.py
The python module of the week page for struct also has some very useful information.