问题标签 [basehttprequesthandler]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1605 浏览

python - 如何将队列放入 Python 2 BaseHTTPRequestHandler?

在 Python 2.7 中,我扩展了 BaseHTTPServer.BaseHTTPRequestHandler 以支持 do_POST 方法。我想给请求处理程序一个队列,以便它可以将发布的数据放在队列中以由另一个线程处理。

这是我班的精简版:

从那时起,处理程序由 BaseHTTPServer 创建,我认为我无法更改init方法以传入队列。

我希望我的 main() 看起来像这样:

0 投票
1 回答
186 浏览

python - How can I safely update the handler of a BaseHTTPServer under Linux?

I'm trying to write a program that changes the content of a webpage with some user given parameters. After several tries and some googling, i've managed to get some results (I put the webserver in a thread in order to continue other tasks in the main routine). It seems to run pretty smooth in windows, but I always got a

socket.error: [Errno 98] Address already in use

when I try it from my linux machine (the whole thing must run on raspian at the end).

I read something here that explains that the SO_REUSEADDR socket option (I think it concerns with) have different behaviours among different OS (and, if I haven't misunderstood, it "works" on Windows thanks to a bug), but I can't find a solution to get it work this way on Linux for this option is already set to True by default.

Here's a simplified absract of the code that generates the error:

with this module (web.py)

Is there a real solution to get rid of this error? Or is there a better way to manage the handler in orger to acchieve my goal?

EDIT: the error occurs after the input of the "2nd content". The 1st instance runs flawlessly, so I can tell for sure that the socket is not used by anything else.

Thanks to all!

0 投票
1 回答
694 浏览

python - BaseHTTPServer 只服务网页一次

我试图弄清楚如何在 Python 中设置一个基本的 Web 服务器,但我遇到了很多困难。

我的主要问题是我只能让我的服务器为网页提供一次服务。html 在浏览器中显示一条消息,而 Javascript 在控制台中显示另一条消息。

当我启动服务器并转到http://127.0.0.1:8080时,我的两条消息都会显示并且一切都很好。但是,当我打开第二个浏览器选项卡并再次访问时遇到了问题。我在终端中收到了 GET HTTP 请求,但没有收到 GET Javascript 请求。浏览器窗口或控制台中都没有显示任何内容。

我究竟做错了什么?任何建议,将不胜感激。

这是我的 Python 代码:

0 投票
1 回答
1960 浏览

python - Python: Retrieve POST response from BaseHTTPRequestHandler

I'm trying to retrieve the response after a POST request with Python using a BaseHTTPRequestHandler. To simplify the problem, I have two PHP files.

jquery_send.php

jquery_post.php

With jquery_send.php, I can send a POST request to jquery_post.php and retrieve the request successfully. Now, I want to get the same result sending the POST request to a Python BaseHTTPRequestHandler instead jquery_post.php. I'm using this Python code for testing:

I can get the POST request, but I can't retrieve the response ("Received!") in jquery_send.php. What am I doing wrong?


EDIT:

In short, I have this little Python code using a BaseHTTPRequestHandler to get a POST request and send a response.

I can get the response with curl

but I can't get it using ajax/jquery from a webpage (the server receives the POST request correcty, but the webpage doesn't retrieve the response). How can I do it?

0 投票
1 回答
362 浏览

python - 如何使用 python httplib、Httpserver 测量 RTT

我使用 httplib 和 BaseHTTPServer 做了一个 HTTP 代理,现在我想知道是否有办法计算到每个服务器的 RTT。我不想使用 ping 方法,因为我想计算通过我的代理的每个对象服务器的 RTT。我可以编辑上面的库来实现我的目标还是已经有类似的东西了?

0 投票
1 回答
1454 浏览

python - 在同一类的另一个方法中使用来自 __init__ 的变量

我试图在init中创建一个变量,但在同一类的其他方法中无法识别它。

我不知道为什么 self.myvarlol 在 send_myheaders() 中无法正常工作:S

代码:

错误:

0 投票
0 回答
2342 浏览

python - 如何从 Python HTTP 服务器调用函数?

我想为我的一个应用程序提供一个 Web 界面。为了实现这一点,我BaseHTTPRequestHandlerBaseHTTPServer包中子类化(到我调用的类中_RequestHandler)并为 HTTPHEAD / GET / POST / PUT请求实现我自己的处理程序。

当我收到对“普通文件”的请求时,我只是打开它,从文件系统中读取它并将其提供给浏览器。这很好用。但是,作为 Web 界面,我还想将某些请求传达给应用程序后端。为此,我希望我的服务器提供一个接口,可以在该接口上注册某些 URL 的回调函数(由后端),当有注册 URL 的请求传入并传递请求头和正文时,将调用该接口到为该 URL 注册的请求处理程序(= 回调函数)。

IE。我希望能够做这样的事情。

现在,当有人通过浏览器请求某个尚未注册的任意 URL 时,Web 服务器将加载指定的文件(相对于“Web 根”)并传送它。

我原则上知道如何实现这一点。将应该以不同方式处理的 URL 放在字典中。对于每个请求,检查请求的 URL 是否在字典中。如果是,则将请求数据(HTTP 标头和正文)放入“请求对象”中,调用为该 URL 注册的回调函数并将该请求对象传递给它。从回调函数中获取响应对象,提取其内容(HTTP 标头和正文)并将其提供给客户端。如果 URL 不在“特殊处理”字典中,只需从文件系统(相对于“Web 根”)加载文件并提供它。

问题是,为了运行 Web 服务器,我必须做这样的事情。

现在的问题是我不能将一个对象,一个实例传递_RequestHandlerHTTPServer. 我必须通过课程本身HTTPServer将为我创建实例,这对我来说是无法访问的,所以我不能在它上面调用方法,因此不能向它注册回调函数(或将回调函数字典传递给构造函数或其他)。

我能做些什么?必须有某种方法可以将自定义数据对象放入_RequestHandler实例中,对吧?我的意思是,如果没有,那么所有服务器的东西似乎都受到了严格的限制。不过,我不知道该怎么做,因为我无权访问_RequestHandler. 它将由HTTPServer班级为我创建,而不是传递给外部。

有什么建议么?

0 投票
2 回答
1726 浏览

python - python - 将 BaseHTTPRequestHandler 与 UnixStreamServer 一起使用会导致异常

我正在尝试创建一个绑定到 Unix 域套接字的基本 HTTP 服务器,但是使用带有 UnixStreamServer 的 BaseHTTPRequestHandler 子类会创建一个异常,表明它们不能一起工作。

玩具服务器.py:

然后我发送一个请求curl --unix-socket ./mysocket.sock http:/hello,这会导致服务器上出现以下异常(使用 Python 2.7.11):

我做了一些挖掘,问题与返回空字符串作为地址的事实有关sock.accept(),大概是因为 UDS 调用中没有客户端地址的概念。然而,BaseHTTPRequestHandler 需要一个用于日志记录(可能还有其他事情)

有没有办法解决这个问题,不涉及猴子补丁或通过这个小改动重新实现 BaseHTTPServer?BaseHTTPRequestHandler 甚至打算与 UnixDataStream 一起使用吗?对 Python 中轻量级 UDS HTTP 服务器的更好建议?

谢谢!

0 投票
1 回答
992 浏览

http - 为什么 do_GET 比 do_POST 快得多

Python 的 BaseHTTPRequestHandler 与通过 post 发送的表单存在问题!

我见过其他人问同样的问题(为什么 GET 方法比 POST 更快?),但在我的情况下时间差太大(1 秒)

Python服务器:

使用python服务器获取请求非常快

印刷

在服务器端:

瞬间!

将表单发送到python服务器时发布请求非常慢

印刷

在服务器端:

具体来说, self.rfile.read(length) 对于非常少的表单数据需要 1 秒

向python服务器发送数据(不是表单)时的发布请求非常快

印刷

在服务器端:

node.js 服务器

将表单发送到 node.js 服务器时的发布请求非常快

印刷:

0 投票
3 回答
17183 浏览

python - Python BaseHTTPRequestHandler:使用 JSON 响应

我有一个继承BaseHTTPRequestHandler并实现方法的 Python 类do_POST

我目前只成功响应整数状态,例如 200,在方法末尾使用以下命令:

我也在尝试发送一些字符串作为响应的一部分。我该怎么做?