How to programmatically communicate with Apache?

别说谁变了你拦得住时间么 提交于 2019-12-11 16:07:43

问题


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.

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.

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)

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

Edit: 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)

回答1:


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

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

import requests

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

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



来源:https://stackoverflow.com/questions/48635552/how-to-programmatically-communicate-with-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!