How to make this Twisted Python Proxy faster?

The following is the HTTP proxy code for filtering content. It uses GET to send the URL of the current site to the server, where it processes it and responds. It works VERY , VERY , VERY slowly. Any ideas on how to make this faster?

Here is the code:

from twisted.internet import reactor
from twisted.web import http
from twisted.web.proxy import Proxy, ProxyRequest
from Tkinter import *
#import win32api
import urllib2
import urllib
import os
import webbrowser

cwd = os.path.abspath(sys.argv[0])[0]
proxies = {}
user = "zachb"
class BlockingProxyRequest(ProxyRequest):
    def process(self):
        params = {}
        params['Location']= self.uri
        params['User'] = user
        params = urllib.urlencode(params)
        req = urllib.urlopen("http://weblock.zbrowntechnology.info/ProgFiles/stats.php?%s" % params, proxies=proxies)
        resp = req.read()
        req.close()
        if resp == "allow":
            pass
        else:
            self.transport.write('''BLOCKED BY ADMIN!''')
            self.transport.loseConnection()

        ProxyRequest.process(self)

class BlockingProxy(Proxy):
    requestFactory = BlockingProxyRequest
factory = http.HTTPFactory()
factory.protocol = BlockingProxy

reactor.listenTCP(8000, factory)
reactor.run()

Anyone have any ideas on how to make this launch faster? Or even the best way to write it?

+5
source share
1 answer

The main reason for the slowness in this proxy is probably these three lines:

    req = urllib.urlopen("http://weblock.zbrowntechnology.info/ProgFiles/stats.php?%s" % params, proxies=proxies)
    resp = req.read()
    req.close()

. , . , , , HTTP-. , HTTP- .

API twisted.web.client(, Agent getPage). API , . .

+11

All Articles