小编Kan*_*dle的帖子

如何在Python中编写Web代理

我正在尝试在python中编写Web代理.我们的目标是访问一个像这样的网址:http://proxyurl/http://anothersite.com/并http://anothersite.com像往常一样看到他的内容.我通过滥用请求库得到了很多,但这并不是请求框架的预期用途.我之前已经编写了扭曲的代理,但我不确定如何将其与我想要做的事情联系起来.这是我到目前为止的地方......

import os
import urlparse

import requests

import tornado.ioloop
import tornado.web
from tornado import template

ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)

loader = template.Loader(path(ROOT, 'templates'))


class ProxyHandler(tornado.web.RequestHandler):
    def get(self, slug):
        if slug.startswith("http://") or slug.startswith("https://"):
            if self.get_argument("start", None) == "true":
                parsed = urlparse.urlparse(slug)
                self.set_cookie("scheme", value=parsed.scheme)
                self.set_cookie("netloc", value=parsed.netloc)
                self.set_cookie("urlpath", value=parsed.path)
            #external resource
            else:
                response = requests.get(slug)
                headers = response.headers
                if 'content-type' in headers:
                    self.set_header('Content-type', headers['content-type'])
                if 'length' in headers:
                    self.set_header('length', headers['length'])
                for block …
Run Code Online (Sandbox Code Playgroud)

python proxy tornado python-requests

21
推荐指数
2
解决办法
1万
查看次数

标签 统计

proxy ×1

python ×1

python-requests ×1

tornado ×1