How to properly return http response from Python Requests library (want a Python reverse proxy)

元气小坏坏 提交于 2020-02-24 20:52:49

问题


This question continues from here.

I want to create a reverse proxy of sorts that will allow me to host an application that runs on a specified port on a server that does not have that port open. But I do not want take over ports 80/443 because I need to have other apps running on that server. Instead, I want an app running at localhost:#### to be served from a suburl or subdomain such as http://myserver/myapp.

Here is what I tried:

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)

When I run helloflask.py it runs on port 5000, and I can visit mydomain/middle.py and get the correct response of "Hello World!" displayed in the browser.

However, when I run a jupyter notebook on port 8888, I get a malformed web page. Strangely, the page sources are identical (see the diff).

So, what do I need to do to make jupyter run from the proxy?

Here is an image of how the page appears through middle.py. The link to IPythonParallel works, but no other links or controls function in any way.

来源:https://stackoverflow.com/questions/48673359/how-to-properly-return-http-response-from-python-requests-library-want-a-python

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