I've been busy with other shit lately so I haven't been programming very much, but a few days ago I started a new project. Basically it's going to be an IRC-esque server/client program that I'm making for fun but mostly to learn PyGTK. This post isn't about that though; this post is about a module that I wrote to handle the backend socket management.
Rather than learn one of the tried-and-true frameworks out there like Twisted I just decided to bang out a lightweight module to do what I need because A) it's fun and B) Twisted has shitty documentation that I don't like reading.
As you probably guessed the module that I wrote is written in Python, and it is used to handle all incoming and outgoing socket connections and messages for both the client and server. Here's an example of a basic echo server and client:
client.py
import pynetty
class MyProtocol(pynetty.client.ClientProtocol):
def on_receive(self, base, data):
print "Server sent '{}'".format(data)
base.sock.send("Hello, server!")
pynetty.working_client = pynetty.client.ClientBase()
pynetty.working_client.protocol = MyProtocol()
pynetty.connect("127.0.0.1", 8000)
server.py
import pynetty
class MyProtocol(pynetty.client.ServerProtocol):
def on_connect(self, base, sock, addr):
print "{}:{} connected".format(*addr)
sock.send("Hello, client!")
def on_disconnect(self, base, sock, addr):
print "{}:{} dropped".format(*addr)
def on_receive(self, base, sock, addr, data):
print "Client sent '{}'".format(data)
pynetty.working_server = pynetty.server.ServerBase("127.0.0.1", 8000)
pynetty.working_server.protocol = MyProtocol()z
pynetty.listen()
It's not finished yet so it doesn't function perfectly. For example if you run that example code the application will never exit; it will just loop forever doing nothing, and the only way to close it will be through the OS.
You can grab the most recent source code from my GitHub repo.
That's about it for now. I'll make another post about my IRC-type program that I'm working on when I make further progress.