python Http proxy

python Http proxy

Postby Hixnal » Tue Nov 19, 2013 2:24 pm

Hi
this Http proxy has written by python (Threading and Socket) but its speed is not well
test it and say way its speed is low ; i think threads are not managed well;
Code: Select all
from socket import *
import threading
from time import sleep
HOST = '127.0.0.1'
PORT = 2002
mx=0
def getheader(headerdata,hdata):
   headerdata=str(headerdata)
   hoststart=headerdata.find(hdata)
   hoststart=hoststart+len(hdata)
   hoststart=int(hoststart)
   hostend=hoststart
   while hostend<=len(headerdata):
      hostend=hostend+1
      char=headerdata[hostend:hostend+1]
      if ord(char)==92:
         return headerdata[hoststart:hostend]
   return "Invalid Data"
def serverhandler(svrdata,clientsock):
        host=getheader(svrdata,"Host: ")
        print (getheader(svrdata,"GET"))
        try:
           servsock = socket(AF_INET, SOCK_STREAM)
           servsock.connect((host, 80))
           servsock.send(svrdata)
        except:
           clientsock.send(b"\n\nInvalid Host")
           return 0
        while 1:
           resserv=servsock.recv(1024)
           if not resserv: break
           clientsock.send(resserv)
        clientsock.close()
        servsock.close()
        return 0
def clienthandler(ADDR,BUFF):
        serversocket = socket(AF_INET, SOCK_STREAM)
        serversocket.bind(ADDR)
        serversocket.listen(1)
        f, addr = serversocket.accept()
        data = f.recv(BUFF)
        global mx
        mx=0
        threading._start_new_thread(serverhandler, (data,f))
        return 0

if __name__=='__main__':
    ADDR = (HOST, PORT)
    BUFF = 2084
    print ("Ready")
    while 1:
       if mx==0:
          threading._start_new_thread(clienthandler, (ADDR,BUFF))
          mx=1

Hixnal
 
Posts: 2
Joined: Tue Nov 19, 2013 2:20 pm

Re: python Http proxy

Postby Hixnal » Wed Nov 20, 2013 4:37 pm

Hey ; help me please??
Hixnal
 
Posts: 2
Joined: Tue Nov 19, 2013 2:20 pm


Return to Networking

Who is online

Users browsing this forum: AndresFelix and 1 guest