如何以编程方式与Apache通信? [英] How to programmatically communicate with Apache?

查看:96
本文介绍了如何以编程方式与Apache通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如今,这么多的Web应用程序都在它们自己的微服务器上运行,很难在共享主机平台上实现它们.这些应用程序监听您可以自定义或反向代理的专用端口,但是共享主机通常只有80和443打开.

So many web applications these days run on their own microservers, it can be hard to implement them on shared hosting platforms. The apps listen on a dedicated port you can customize or reverse proxy, but shared hosting usually only has 80 and 443 open.

仅作为示例,基于Web的便捷编辑器 ICEcoder 是一个PHP应用程序,因此您只需删除目录中的文件,然后就走了.但是, Cloud9 编辑器将运行其自己的服务器.您可以自定义端口,但是同样,您不能运行反向代理.

Just as an example, the handy web-based editor ICEcoder is a PHP application, so you just drop the files in a directory and away you go. However, the Cloud9 editor runs its own server. You can customize the port, but again, you cant run the reverse proxy.

我想到了使用PHP或Python CGI脚本作为中介的想法.像这样:

I had the idea of using a PHP or Python CGI script as an intermediary. Something like:

www.mydomain/mydirectory/middleman.py

from BaseHTTPServer import BaseHTTPRequestHandler
import urlparse, json
# hpyothetical apache api
import apache

parsed_path = urlparse.urlparse(self.path)

response = apache(url=parsed_path, port=8080)

sendStuffBack(response)

使用Apache可以做到吗?我将如何实施?

Would this be possible with Apache? How would I implement it?

修改: 这是我根据@grawity的回答所做的事情.

Here is what I did based on @grawity's answer.

helloflask.py

#!/usr/bin/env python

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

middle.py

#!/usr/bin/env python

print ("Content-Type: text/html")
print()

import requests
#response = requests.get("http://localhost:5000")
response = requests.get("http://localhost:8888/token=8a387fe88d662e2568f9b8ec2398191452492e7184536670")

print(response.text)

推荐答案

您的Python项目是反向代理,而您正在寻找的API只是普通的HTTP. (毕竟,这就是网络浏览器已经与Apache交互的方式...)

Your Python project is a reverse proxy, and the API you're looking for is just ordinary HTTP. (After all, that's how web browsers interact with Apache already...)

要发出HTTP请求,您需要一个诸如urllibrequests的客户端:

To make HTTP requests, you need a client like urllib or requests:

import requests

response = requests.get("http://" + apache_host + ":8080/" + parsed_path)

默认情况下,所有应用程序和微服务器都将认为所有客户端都来自本地主机.如果存在问题,请查看您的应用是否接受 X-Forwarded-For 标头. (如果有的话,请将其包含在您的所有请求中.)

By default, all your apps and microservers will think that all clients come from localhost. If that's a problem, see if your apps accept the X-Forwarded-For header. (If they do, include it in all your requests.)

这篇关于如何以编程方式与Apache通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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