IHMO is best done with the Asynchornous I / O library / framework. Here's a solution using circuits :
The echos server receives what it receives in stdout, and the client opens the file and sends it to the server, waiting for it to complete before the socket is closed and completed. This is done with a mixture of Async I / O and Coroutines.
server.py:
from circuits import Component from circuits.net.sockets import UNIXServer class Server(Component): def init(self, path): UNIXServer(path).register(self) def read(self, sock, data): print(data) Server("/tmp/server.sock").run()
client.py:
import sys from circuits import Component, Event from circuits.net.sockets import UNIXClient from circuits.net.events import connect, close, write class done(Event): """done Event""" class sendfile(Event): """sendfile Event""" class Client(Component): def init(self, path, filename, bufsize=8192): self.path = path self.filename = filename self.bufsize = bufsize UNIXClient().register(self) def ready(self, *args): self.fire(connect(self.path)) def connected(self, *args): self.fire(sendfile(self.filename, bufsize=self.bufsize)) def done(self): raise SystemExit(0) def sendfile(self, filename, bufsize=8192): with open(filename, "r") as f: while True: try: yield self.call(write(f.read(bufsize))) except EOFError: break finally: self.fire(close()) self.fire(done()) Client(*sys.argv[1:]).run()
In my testing, this behaves exactly as I expect, errors and servers receive the full file before the client clsoes socket on and off.
source share