Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make this Twisted Python Proxy faster?

The code below is an HTTP proxy for content filtering. It uses GET to send the URL of the current site to the server, where it processes it and responds. It runs VERY, VERY, VERY slow. Any ideas on how to make it 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 run faster? Or even a better way to write it?

like image 708
Zac Brown Avatar asked Nov 18 '10 03:11

Zac Brown


People also ask

How to speed up your Python code?

Python’s built-in functions are one of the best ways to speed up your code. You must use built-in python functions whenever needed. These built-in functions are well tested and optimized. The reason these built-in functions are fast is that python’s built-in functions, such as min, max, all, map, etc., are implemented in the C language.

Should I use PyPy or CPython for speed?

However, because it’s a high-level interpreted language, CPython has certain limitations and won’t win any medals for speed. That’s where PyPy can come in handy. Since it adheres to the Python language specification, PyPy requires no change in your codebase and can offer significant speed improvements thanks to the features you’ll see below.

Is Python too slow?

Python is flexible, but it can be slow. Let’s speed it up. Python is one of my favorite languages to work with. It is easy to learn, has an excellent selection of open source libraries, and has an extremely active and helpful community.

How much faster is join() in Python?

The second approach is nearly 2 times faster. In Python, strings are immutable so you cannot modify them. Each time you concatenate strings, a new string is created. As expected, this situation results in some performance issues. Using join () makes our program 4 times faster.


1 Answers

The main cause of 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()

A normal Twisted-based application is single threaded. You have to go out of your way to get threads involved. That means that whenever a request comes in, you are blocking the one and only processing thread on this HTTP request. No further requests are processed until this HTTP request completes.

Try using one of the APIs in twisted.web.client, (eg Agent or getPage). These APIs don't block, so your server will handle concurrent requests concurrently. This should translate into much smaller response times.

like image 179
Jean-Paul Calderone Avatar answered Nov 12 '22 03:11

Jean-Paul Calderone