I wanted to make a very simple proxy using python (mostly to understand how it works). I am talking about a generic TCP proxy, not only http. I have built the following code, however, it seems to only work one way : ie the request is sent but I never get the answer. Here is the code :

import socket
import argparse

#Args
parser = argparse.ArgumentParser(description='ProxyDescription')
parser.add_argument('-l', '--listen', action='store', help='Listening port', default=80, type=int)
parser.add_argument('destination', action='store', help='Destination host')
parser.add_argument('port', action='store', help='Destination port', type=int)
args = parser.parse_args()

#Server
s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s1.bind(('', args.listen))
s1.listen(1)
conn1, addr1 = s1.accept()

#Client
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2.connect((args.destination, args.port))
s2.setblocking(0)

print "Connected by ", addr1
while 1:
    datato = conn1.recv(1024)
    if not datato: 
        print "breaking to"
        break
    s2.send(datato)
    print "data send : " + datato
    try:
        datafrom = s2.recv(1024)
        print "reveived data : " + datafrom   
        if not datafrom:
            print "breakinf from"
            break
        print "datafrom: " + datafrom
        conn1.send(datafrom)
    except socket.error, msg:
        print "error rcving: " + str(socket.error) + " || " + str(msg) 
        continue
print "the end ... closing"
conn1.close()
s2.close()

My test is simply launching this script and telnet through it. If I look with wireshark, I see that the request is fully understood by the server and I do get an answer, however, I never get the answer in the telnet. (testing with simple GET / on google) I feel the problem is related to "blocking" / "non-blocking" socket but I can't really understand where it is.

Edit
Report