torayeff
torayeff

Reputation: 9702

Twisted simple HTTP proxy contd

I have found this script on this site:

from twisted.web import proxy, http
from twisted.internet import reactor
import sys

class MyProxy(proxy.Proxy):

    def dataReceived(self, data):
      print data 
      return proxy.Proxy.dataReceived(self, data)

class ProxyFactory(http.HTTPFactory):
  protocol=MyProxy

factory = ProxyFactory()
reactor.listenTCP(8080, factory)
reactor.run()

Here as you see I override dataReceived method to print data. This, when run, prints to stdout every requested header:

GET http://careers.stackoverflow.com/ad/i/nNxudq0-kvjnJ84-n6osrC0-12-vYY HTTP/1.1
Host: careers.stackoverflow.com
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Proxy-Connection: keep-alive
Referer: http://stackoverflow.com/questions/7052849/simple-http-proxy
Cookie: __utma=140029553.285085787.1331510700.1337692646.1337711538.33; __utmz=140029553.1337711538.33.19.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); __qca=P0-608923218-1331510699748; usr=t=5TLQ0kWmkGJo&s=RgkodeSUGq8k; __utmc=140029553; __utmb=140029553.3.10.1337711538
  1. Is it possible to override this in such a way (or any other implementation), that I can access received data (headers) as dictionary, (ex: data['Host'] = 'xxxx' ...)
  2. And also I want to get all urls from this page.

Upvotes: 1

Views: 510

Answers (1)

dda
dda

Reputation: 6213

Since you are getting the raw data, test whether each line is a header (/^[-a-zA-Z]+: / sounds like a good start; also watch out for the double crlf that signifies the end of the headers), and store that into a dictionary yourself.

Upvotes: 1

Related Questions