#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Python Http proxy


    Hi
    i have written a Http proxy server by python(Threading and socket) it seems its speed is not well;
    test it and say me why;
    i think threads are not managed well;

    Code:
    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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Help


    is any bodyhelp me ??
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    I tested your program, python3, windows 7. It consumes 1 cpu "core" until I externally kill the python process.

    The program does not display "ready" unless I flush stdout, which surprises me.
    Code:
        print("Ready")
        sys.stdout.flush()
    I have no more to offer you now.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    proxy


    so ; what i have to do for more proformance ?? and working with multi-core cpu ??
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    why do you think the the program performs slowly?


    When run on linux your program responds

    ready

    right away.


    There's your answer. Discard all things Microsoft.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0
    my program has to run on windows ; its right its speed is well on Linux ; but when this script is complete that its speed become good on both windows and linux ;
    i dont know what i have to do that its speed become well on windows . i made a proxy server on twisted and its speed was good. and i dont want to use microsofts productions ;
    whats your edia to be better speed??
    thank you for your help

IMN logo majestic logo threadwatch logo seochat tools logo