patternpythonMinor
SOXXY: a daemon stated IRC client project
Viewed 0 times
soxxyclientprojectircstateddaemon
Problem
This script is basically a proxy service; except it doesn't require an active client connection. In fact it expects the client connection to connect before, and disconnect after every transaction. Once running the proxy will buffer all incoming bytes, and will dump those bytes to the client upon request. Before I go any further and begin working on the IRC client intended to be used with this, I am posting here to persuade any feedback regarding the project in its current state from readers here:
soxxy.py
```
#! /usr/bin/python3
from socket import socket
from threading import Thread as thread
from sys import getdefaultencoding as enc
from sys import byteorder
from sys import argv
enc = enc()
CNCT, SEND, RECV, DISC, STAT = 0, 1, 2, 3, 4
class sox:
def recv_t(self, s):
while self.run:
try: self.buff += s.recv(8192)
except: continue
def process(self, lo):
try: bs = lo[0].recv(8192)
except: return
op, data = bs[0], bs[1:]
if op is CNCT:
data = str(data, enc)
host, port = data.split(":")
self.sock = socket()
self.sock.connect((host, int(port)))
self.run = True
thread(target=self.recv_t, args=(self.sock, )).start()
elif op is SEND: self.sock.send(data)
elif op is RECV: lo[0].send(self.buff); self.buff = bytes()
elif op is DISC: self.run = False; self.sock.close()
elif op is STAT: bytes("error: %s" % (str(self.serr), ), enc)
def server(self, port, host):
ss = socket()
ss.bind((host, int(port)))
while True:
ss.listen(1)
self.process(ss.accept())
def __init__(self, argv):
self.sock = None
soxxy.py
```
#! /usr/bin/python3
from socket import socket
from threading import Thread as thread
from sys import getdefaultencoding as enc
from sys import byteorder
from sys import argv
enc = enc()
CNCT, SEND, RECV, DISC, STAT = 0, 1, 2, 3, 4
class sox:
def recv_t(self, s):
while self.run:
try: self.buff += s.recv(8192)
except: continue
def process(self, lo):
try: bs = lo[0].recv(8192)
except: return
op, data = bs[0], bs[1:]
if op is CNCT:
data = str(data, enc)
host, port = data.split(":")
self.sock = socket()
self.sock.connect((host, int(port)))
self.run = True
thread(target=self.recv_t, args=(self.sock, )).start()
elif op is SEND: self.sock.send(data)
elif op is RECV: lo[0].send(self.buff); self.buff = bytes()
elif op is DISC: self.run = False; self.sock.close()
elif op is STAT: bytes("error: %s" % (str(self.serr), ), enc)
def server(self, port, host):
ss = socket()
ss.bind((host, int(port)))
while True:
ss.listen(1)
self.process(ss.accept())
def __init__(self, argv):
self.sock = None
Solution
I believe
Rather than a long
Since there's effectively no exception handling it will be difficult to diagnose errors.
Using
enc = enc() is considered an anti-pattern, because the semantics of enc changes. I'd suggest renaming the variable to encoding.Rather than a long
if..elif..else block you could use a simple handle_operation(op) call.Since there's effectively no exception handling it will be difficult to diagnose errors.
Using
argparse would make it possible to understand how to use the code without reading it. As a bonus it adds -h/--help support by default so users can see what the expected call structure is.sleeping to wait for an end point to be available is an anti-pattern. To guarantee that an end point is up and that you get started processing as quickly as possible it's common to poll the end point until it responds correctly or fail if there's a timeout.Context
StackExchange Code Review Q#132697, answer score: 2
Revisions (0)
No revisions yet.