- 전체
- Python 일반
- Python 수학
- Python 그래픽
- Python 자료구조
- Python 인공지능
- Python 인터넷
- Python SAGE
- wxPython
- TkInter
- iPython
- wxPython
- pyQT
- Jython
- django
- flask
- blender python scripting
- python for minecraft
- Python 데이터 분석
- Python RPA
- cython
- PyCharm
- pySide
- kivy (python)
Python 인터넷 [Python 인터넷] Using a Python HTTP Proxy Server to Simulate an API Gateway
2023.03.11 20:51
[Python 인터넷] Using a Python HTTP Proxy Server to Simulate an API Gateway

Our software development team uses Azure API Management as an API Gateway. This API Gateway is responsible for forwarding requests to the appropriate backend service. It also applies various inbound and outbound policies, such as validating the presence of a well formed JWT, IP filtering, and attaching request/response headers.
When integration testing our API backends, we want to use a single base URL when making requests to the API. (This is as opposed to keeping track of which endpoints point to which backend in the API client itself.) We also want to ensure that any HTTP headers the API backend expects to have been provided through the API Gateway have been populated.
To achieve this, we wrote a pretty simple HTTP Proxy server in Python. Here, I’ll walk through creating and running a simplified version of our Python proxy server.
Creating an Initial Proxy Server
We will extend the BaseHTTPRequestHandler
from http.server to create our HTTP Server as follows:
Each HTTP method will point to a common request handler that sends the request to the appropriate backend URL and passes back the response.
The _handle_request
function will also set additional custom request headers. In this example, we update the headers with API versions and path prefixes. You can use these, say, when constructing locations for created responses.
Mapping Routes
Next, we will implement the _resolve_url
function to map requests to their appropriate backend API handlers. We use routes.mapper to group endpoints sharing a common backend together. In this simplified example, we have two services, “users” and “organizations”, so we add the following to the top of the file:
With our mapper set up, we can now implement _resolve_url
.
Note that the implementation of get_base_url_for_service
will depend on how you are storing (env variables, hardcoded, etc.) the host names, ports, and any path prefixes.
Running the Server
We run our proxy server from Docker. Add the following simple Dockerfile:
With that in place, we can add a new “proxy” service to our docker-compose.yaml
, making sure to include each backend service as dependencies.
Conclusion
Using a proxy server like this has helped our team confine request forwarding and transformation into a single place so that our API clients only have to worry about a single base URL. I hope this simple Python proxy server can act as a good starting point for wiring up some of the functionality that would ordinarily be provided through an API Gateway such as Azure’s API Management.
[출처] https://spin.atomicobject.com/2022/07/18/python-http-proxy-server/
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.