python - Connect two sockets together without writing byte-manipulating plumbing code? -
in python, possible connect 2 sockets without writing byte-manipulating plumbing code?
for example, write program interacts user (request/response format) , after that, performs tcp connection host , hands on stdin/stdout sockets.
so data received on stdin sent on tcp socket, , data received tcp socket sent stdout - simultaneously, instantly, without either of them blocking.
what recommended way of doing this? i'd avoid writing load of socket code if possible , have 'just work'.
edit: first post didn't answer op wanted. revised significantly.
""" open terminal , run nc -l 8080 type in both terminals> """ import sys socket import socket, af_inet, sock_stream select import select host = 'localhost' port = 8080 sock = socket(af_inet, sock_stream) sock.connect((host, port)) reader = sock.makefile('r', 0) writer = sock.makefile('w', 0) while true: ins, _, _ = select([reader, sys.stdin],[],[]) in ins: if == reader: sys.stdout.write(i.read(1)) if == sys.stdin: writer.write(sys.stdin.read(1))
Comments
Post a Comment