import optparse
import socket, select, threading
class Pipe(threading.Thread):
time_out = 5
buffer_size = 4096
max_idle = 5
def __init__(self, server_host, server_port, dclient_connxn):
super(Pipe, self).__init__()
self.server_host = server_host
self.server_port = server_port
self.server_addr = (server_host, server_port)
self.client_connxn = client_connxn
def connect_to_server(self):
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(self.server_addr)
self.server_sock.connect(self.server_addr)
def log_request_response(self, req, resp):
print('request')
print(req)
print('response')
print(resp)
def run(self):
self.connect_to_server()
to_read = [self.client_connxn, self.server_sock]
to_write = []
to_watch_for_exception = to_read
request = ''
response = ''
seconds_idle = 0
while True:
(ready_to_read, ready_to_write, experienced_exception) = select.select(to_read, to_write, to_watch_for_exception, 1)
if (experienced_exception):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break
if (ready_to_read):
seconds_idle = 0
for waitable in ready_to_read:
data = waitable.recv(buffer_size)
if (waitable == self.client_connxn):
out = self.server_sock
request += data
else:
out = self.client_connxn
log_to = response
response += data
out.send(data)
else:
seconds_idle += 1
if (seconds_idle == max_idle):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break
class PortListener():'''
'''
def __init__(self, listen_on_port, pipe_to_host, pipe_to_port, max_connxns=1):
self.listen_on_port = listen_on_port
self.pipe_to_host = pipe_to_host
self.pipe_to_port = pipe_to_port
def listen():
listen_to_addr = ('127.0.0.1', self.listen_to_port)
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.bind(listen_to_addr)
listen_socket.listen(max_connxns)
while True:
# pause until client requests connection
(client_connxn, client_addr) = listen_socket.accept()
pipe = Pipe(self.pipe_to_host, self.pipe_to_port, client_connxn)
pipe.start()
def setup_info_logger(log_file_path):
logging.basicConfig(filename=log_file_path, level=logging.INFO)
return logging.getLogger()
def parse_command_line():'''
parse command line argument to populate dictionary
return dictionary with keys = client_port, server_host, server_port, log_file_path
returns None if mandatory option missing
'''
parser = optparse.OptionParser()
parser.add_option('--clientport', dest='client_port', help='port client to this port on localhost')
parser.add_option('--serverhost', dest='server_host', help='server host, default = 127.0.0.1')
parser.add_option('--serverport', dest='server_port', help='server port')
parser.add_option('--logfilepath', dest='log_file_path', help='log file path')
(options, args) = parser.parse_args()
if (options.client_port == None):
print('--clientport is mandatory')
return None
if (options.server_port == None):
print('--serverport is mandatory')
return None
d = {}
if (options.log_file != None): d['log_file'] = options.log_file
else: d['log_file'] = None
d['client_port'] = options.client_port
if (options.server_host != None): d['server_host'] = options.server_host
else: d['server_host'] = '127.0.0.1'
d['server_port'] = options.server_port
return d
def main():
opt = parse_command_line()
logger = None
if (opt['log_file_path'] != None):
logger = setup_info_logger(opt['log_file_path'])
listener = PortListener(opt['client_port'], opt['server_host'], opt['server_port']]
if (__name__ == '__main__'):
main()
Monday, August 8, 2011
TCP/IP Port Pipe - UNDER CONSTRUCTION
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
comment: