【问题标题】:Making an tornado proxy page制作龙卷风代理页面
【发布时间】:2014-02-05 08:04:22
【问题描述】:

我想做一个龙卷风代理页面。

所以当我访问proxypage时,我会从google.com获取内容。

我在 php 中制作了一个小的简单代理页面,只是为了发现 tornado 网络服务器不支持 php。 这是 php 代码,可以更好地解释我在寻找什么:

<?php
$file = file_get_contents($_GET['requrl']);
echo $file;
?>

所以没有什么过分的。

我正在向一个提供 json 响应的站点发出 ajax 请求,没有图像,没有什么特别的,只有 json。但 ajax 不适用于跨域请求(同源策略)。而且站点不支持jsonp或cors,所以代理页面是唯一的方法。

【问题讨论】:

标签: python proxy tornado webproxy


【解决方案1】:

添加一个看起来像这样的处理程序:

(r"/", proxyHandler ),

和处理程序类

class proxyHandler(RequestHandler):
    def get(self):
        connection = httplib.HTTPConnection( "www.google.com" )
        connection.request( 'GET', '[uri]', '[body]', '[header]' )
        response = connection.getresponse()

        if response.status !=200:
            print response.status, response.reason

        data = response.read()
        connection.close()

        try:
            dataJSON = json.loads(data)
            if DEBUG_HTTP:
                printDoc( dataJSON, 'http response json')

            return dataJSON
        except Exception, e:
            print 'data is of type, '+str(type(data))+', returning as is'
            return data

在 connection.request() 调用中,uri 参数不是可选的(可以传递 '' ),body 和 header 是。 这也可以异步方式完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多