在 Python 中结合 SimpleXMLRPCServer 和 BaseHTTPRequestHandler
因为JavaScript中无法跨域xmlrpc请求 我需要创建一个 Python 应用程序,它通过 HTTP 公开一些 HTML,并在同一域上公开 XML-RPC 服务。
在 python 中创建 HTTP 请求处理程序和 SimpleXMLRPCServer 非常容易, 但它们都必须监听不同的端口,这意味着不同的域。
有没有办法创建一些可以在本地主机上的单个端口上侦听的东西 并公开 HTTPRequestHandler 和 XMLRPCRequest 处理程序?
现在我有两个不同的服务:
httpServer = HTTPServer(('localhost',8001), HttpHandler);
xmlRpcServer = SimpleXMLRPCServer(('localhost',8000),requestHandler=RequestHandler)
更新
- 我无法在设备上安装 Apache
- 托管页面将是单个 html 页面
- 唯一的客户端将是 python 服务自行运行的设备
Because cross-domain xmlrpc requests are not possible in JavaScript
I need to create a Python app which exposes both some HTML through HTTP and an XML-RPC service on the same domain.
Creating an HTTP request handler and SimpleXMLRPCServer in python is quite easy,
but they both have to listen on a different port, which means a different domain.
Is there a way to create something that will listen on a single port on the localhost
and expose both the HTTPRequestHandler and XMLRPCRequest handler?
Right now I have two different services:
httpServer = HTTPServer(('localhost',8001), HttpHandler);
xmlRpcServer = SimpleXMLRPCServer(('localhost',8000),requestHandler=RequestHandler)
Update
- I cannot install Apache on the device
- The hosted page will be a single html page
- The only client will be the device on witch the python service runs itself
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们都是
SocketServer.TCPServer
的子类。必须有某种方式来重构它们,以便服务器实例可以分派给两者。一个更简单的替代方案可能是将
HTTPServer
保留在前面,并将 XML RPC 代理到SimpleXMLRPCServer
实例。Both of them subclass of
SocketServer.TCPServer
. There must be someway to refactor them so that once server instance can dispatch to both.An easier alternative may be to keep the
HTTPServer
in front and proxy XML RPC to theSimpleXMLRPCServer
instance.解决方案实际上非常简单,根据 Wai Yip Tung 的回复:
我所要做的就是继续使用 SimpleXMLRPCServer 实例,
但修改处理程序:
这将导致处理程序响应 GET 请求以及原始 POST (XML-RPC) 请求。
The solution was actually quite simple, based on Wai Yip Tung's reply:
All I had to do was keep using the SimpleXMLRPCServer instance,
but modify the handler:
This will cause the handler to respond to GET requests as well as the original POST (XML-RPC) requests.
使用
HTTPServer
提供内容并不是一个好主意。您应该使用 Apache 等网络服务器,并使用 Python 作为 CGI(或更高级的接口,如mod_wsgi
)。然后,Web 服务器在一个端口上运行,您可以直接通过 Web 服务器提供 HTML 服务,并用 Python 编写任意数量的 CGI 脚本,例如使用
CGIXMLRPCRequestHandler
的 XMLRPC 请求示例。Using
HTTPServer
for providing contents is not a good idea. You should use a webserver like Apache and use Python as CGI (or a more advanced interface likemod_wsgi
).Then, the webserver is running on one port and you can server HTML directly over the webserver and write as many CGI scripts as you like in Python, as example one for XMLRPC requests using
CGIXMLRPCRequestHandler
.