LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2013, 10:56 AM   #1
hixna
LQ Newbie
 
Registered: Nov 2013
Posts: 1

Rep: Reputation: Disabled
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
 
Old 11-22-2013, 09:42 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by hixna View Post
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
I'm sorry, but if you wrote this code, shouldn't you also be able to debug it?
 
Old 11-22-2013, 11:36 AM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by hixna View Post
test it and say me why;
Sorry, but that's not a reasonable request.

What exactly do you mean by "slow", and where are you detecting the bottleneck?

Do you have benchmarking or profiling results?

Last edited by dugan; 11-22-2013 at 11:39 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] http proxy for qq client nico34 Linux - Server 2 06-23-2012 01:48 AM
Can i tunnel http via ssh without setting up a http proxy? ginda Linux - Networking 3 12-24-2009 04:25 AM
Using a http proxy from the terminal s0n|k Ubuntu 2 03-16-2007 09:07 AM
proxy via http sharadshankar Linux - Software 2 02-15-2006 05:57 PM
"socks5" -> "http" proxy protocol, or ssh tunnel to sock5 ? I'm beyond http p vmicho Linux - Networking 2 12-16-2003 05:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration