- 전체
- 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/
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 11 |
[PyQT5] PyQt5 Mainwindow에 Qt Designer를 사용한 graph Widget 추가
| 졸리운_곰 | 2024.06.06 | 476 |
| 10 |
[PyQT5] [Python with Pyqt5] Widget에다가 그래프 넣기 (Feat. Matplotlib)
| 졸리운_곰 | 2024.06.06 | 437 |
| 9 |
[PyQT5] 파이썬(Python)PyQt5 - QMessageBox 사용하기
| 졸리운_곰 | 2024.06.06 | 455 |
| 8 |
[PyQT5] UI Designer 에서 Tab Widget 생성 하기
| 졸리운_곰 | 2024.06.06 | 420 |
| 7 |
[PyQT5] UI Designer 에서 리사이즈 시 같이 확장하기
| 졸리운_곰 | 2024.06.06 | 537 |
| 6 |
[PyQT5] UI Designer 에서 Grid Layout 배치 해보기
| 졸리운_곰 | 2024.06.06 | 474 |
| 5 |
[pyQT] 「Python : PyQt5」 Qt Designer : .ui → .py 변환
| 졸리운_곰 | 2024.06.05 | 624 |
| 4 |
[pyQT] Dev/python/ [pyqt5] 프로그램창을 항상 가장 위에 있게 하면서 동시에 타이틀 바도 없게 하려면?
| 졸리운_곰 | 2024.06.01 | 542 |
| 3 |
[pyQT] QtDesigner에서 리소스 편집기 사용하기
| 졸리운_곰 | 2024.05.28 | 499 |
| 2 |
[pyQT] Qt Resource 파일 (.qrc) 적용방법
| 졸리운_곰 | 2024.05.28 | 421 |
| 1 | [pyQT] from PyQt5.QtChart import QLineSeries, QChart, QValueAxis, QDateTimeAxis ImportError: DLL load failed while importing QtChart: 지정된 모듈을 찾을 수 없습니다. | 졸리운_곰 | 2024.01.28 | 430 |

