socket.makefile issues in python 3 while creating a http proxy -


from socket import * import sys  # create server socket, bind port , start listening tcpsersock = socket(af_inet, sock_stream) serverport = 12000 tcpsersock.bind(('', serverport)) tcpsersock.listen(1) print ("server ready")  while 1==1:   # start receiving data client. e.g. request = "get http://localhost:portnum/www.google.com"    tcpclisock, addr = tcpsersock.accept()   print ('received connection from:', addr)   request = str(tcpclisock.recv(1024).decode())   print ("requested " + request)   # extract file name given request   filename = request.split()[1]   print ("file name " + filename)    fileexist = "false"   filetouse = "/" + filename    print ("file use: " + filetouse)   try:     # check wether file exist in cache. open fail , go "except" in case file doesn't exist. similar try/catch in java     f = open(filetouse[1:], "r")                           outputdata = f.readlines()                             fileexist = "true"     # proxyserver finds cache hit , generates response message     tcpclisock.send("http/1.1 200 ok\r\n")                 tcpclisock.send("content-type:text/html\r\n")     tcpclisock.send(outputdata)     print ('this read cache')         except ioerror:     if fileexist == "false":          # create socket on proxyserver         c = socket(af_inet, sock_stream)         hostn = filename.replace("www.","",1) #max arg specified 1 in case webpage contains "www." other usual 1                   print (hostn)                                            try:             # connect socket port 80             c.bind(('', 80))             # create temporary file on socket , ask port 80 file requested client             print("premake")              fileobj = c.makefile('r', 0)              print("postmake")             fileobj.write("get " + "http://" + filename + " http/1.1\r\n")               # read response buffer             print("post write")             buff = fileobj.readlines()             # create new file in cache requested file.              tmpfile = open("./" + filename,"wb")             # send response in buffer both client socket , corresponding file in cache             line in buff:                 tmpfile.write(line)             tcpclisock.send(tmpfile)            except:             print ("illegal request")             break                                                    else:         # http response message file not found         print("http response not found")  # close client , server sockets     tcpclisock.close() #tcpsersock.close() 

the code never manages execute 'try' entered in 'except ioerror'. problem seems socket.makefile(mode, buffsize) function, has poor documentation python 3. tried passing 'rwb', 'r+', 'r+b' , on function, @ manage create file , unable write thereafter.

this python2.7 vs python3 issue. while makefile('r',0) works in python 2.7, need makefile('r',none) in python3.

from documentation python2.7: socket.makefile([mode[, bufsize]])

from documentation python3: socket.makefile(mode='r', buffering=none, *, encoding=none, errors=none, newline=none)


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -