真的很简单的Python HTTP代理吗?

发布于 2024-10-07 01:38:18 字数 489 浏览 0 评论 0原文

我到处都找过,发现了数以百万计的Python代理服务器,但没有一个完全符合我的要求(我认为:s)

我对Python有相当多的经验,但我对黑暗的世界还很陌生HTTP 协议的秘密。

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

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

如果这没有多大意义,我很抱歉,因为我对我的问题的想法可能是混乱的:(

如果你能想到任何能让我的问题更清楚的东西,请建议我添加它。xxx

编辑:

另外,链接到解释代理处理请求、请求页面(此时如何读取/修改数据)并将其传递给原始请求者的详细过程将不胜感激 xxxx

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

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.

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 :(

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

Edit:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-10-14 01:38:18

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

这里有一个非常简单的代理示例:SimpleHTTPServer 模块

它的核心只有 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 读取到的所有内容复制到响应中。

请注意,这确实是最小的。我相信它根本不处理标题。

顺便说一句: path 记录在 http://docs.python.org /library/basehttpserver.html。它是在调用 do* 方法之前设置的。

"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.

There's a really simple proxy example here: The SimpleHTTPServer module.

The core of it is just 3 lines:

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

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: path is documented at http://docs.python.org/library/basehttpserver.html. It was set before your do* method was called.

夏有森光若流苏 2024-10-14 01:38:18

来自 扭曲 Wiki

from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)

class ProxyFactory(http.HTTPFactory):
    protocol = proxy.Proxy

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

From the twisted Wiki

from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)

class ProxyFactory(http.HTTPFactory):
    protocol = proxy.Proxy

reactor.listenTCP(8080, ProxyFactory())
reactor.run()
等风来 2024-10-14 01:38:18

proxpy 看起来很有前途,调整请求和响应非常简单。

proxpy looks rather promising, it's very simple to tweak requests and responses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文