认真简单的python HTTP代理? [英] seriously simple python HTTP proxy?

查看:128
本文介绍了认真简单的python HTTP代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找并发现了数以百万计的python代理服务器,但没有一个正是我想要的(我认为:s)

I have looked everywhere and found millions of python proxy servers but none do precisely what i would like (i think :s)

我有很多一般来说,使用python的经验,但我对HTTP协议的深层秘密世界都很陌生。

I have had quite a bit of experience with python generally, but i'm quite new to the world of the deep dark secrets of the HTTP protocol.

我认为可能有用的将是一个非常简单的可以连接到的代理示例然后它本身会尝试连接到传递给它的地址。

What i think might be useful would be a very simple proxy example that can be connected to and will then itself try to connect to the address passed to it.

另外,我认为令我困惑的是隐藏的东西正在做,例如如果该类继承自BaseHTTPServer.BaseHTTPRequestHandler,那么在请求页面时会发生什么样的事情,就像我发现的许多例子中没有引用路径变量那么突然噗! self.path用于函数中。我假设它已被继承,但它最终如何使用所使用的路径?

Also, i think what has been confusing me is everything the hidden stuff is doing, e.g. if the class inherits from BaseHTTPServer.BaseHTTPRequestHandler what precisely happens when a page is requested, as in many of the examples i have found there is no reference to path variable then suddenly poof! self.path is used in a function. im assuming it's been inherited, but how does it end up with the path used?

对不起如果这没有多大意义,因为我对我的问题的想法是可能是炒作:(

im sorry if that didn't make much sense, as my idea of my problem is probably scrambled :(

如果你能想到任何可以让我的问题更清楚的话,请建议我添加它.xxx

if you can think of anything which would make my question clearer please, please suggest i add it. xxx

编辑:

此外,链接到代理处理请求的详细流程的说明,请求页面(如何读取/修改将此数据传递给原始请求者将非常感谢xxxx

Also, a link to an explaination of the detailed processes through which the proxy handles the request, requests the page (how to read/modify the data at this point) and passes it to the original requester would be greatly appreciated xxxx

推荐答案

一个非常简单的代理示例,可以连接到,然后它自己会尝试连接到传递给它的地址。这实际上是HTTP代理的定义。

"a very simple proxy example that can be connected to and will then itself try to connect to the address passed to it." That is practically the definition of an HTTP proxy.

真的有 这里的简单代理示例: http://effbot.org/librarybook/simplehttpserver.htm

There's a really simple proxy example here: http://effbot.org/librarybook/simplehttpserver.htm

它的核心只有3行:

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.copyfile(urllib.urlopen(self.path), self.wfile)

所以这是一个 SimpleHTTPRequestHandler ,为了响应GET请求,打开URL路径(对代理的请求通常看起来像GET http://example.com/ ,而不是像GET /index.html )。然后它只是将从该URL读取的任何内容复制到响应中。

So it's a SimpleHTTPRequestHandler that, in response to a GET request, opens the URL in the path (a request to a proxy typically looks like "GET http://example.com/", not like "GET /index.html"). It then just copies whatever it can read from that URL to the response.

请注意,这真的最小。我相信它根本不涉及标题。

Notet that this is really minimal. It doesn't deal with headers at all, I believe.

BTW:路径记录在 http://docs.python.org/library/basehttpserver.html 。它是在调用 do * 方法之前设置的。

BTW: path is documented at http://docs.python.org/library/basehttpserver.html. It was set before your do* method was called.

这篇关于认真简单的python HTTP代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆