非常简单的python HTTP代理? [英] seriously simple python HTTP proxy?

查看:26
本文介绍了非常简单的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.

顺便说一句:path 记录在 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天全站免登陆