
- **Add SPDX license headers to python source files** - **Check for SPDX headers using pre-commit** commit 9d7ef44c3cfb72ca4c32e1c677d99259d10d4745 Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:18:24 2025 -0500 Add SPDX license headers to python source files This commit adds SPDX license headers to python source files as recommended to the project by the Linux Foundation. These headers provide a concise way that is both human and machine readable for communicating license information for each source file. It helps avoid any ambiguity about the license of the code and can also be easily used by tools to help manage license compliance. The Linux Foundation runs license scans against the codebase to help ensure we are in compliance with the licenses of the code we use, including dependencies. Having these headers in place helps that tool do its job. More information can be found on the SPDX site: - https://spdx.dev/learn/handling-license-info/ Signed-off-by: Russell Bryant <rbryant@redhat.com> commit 5a1cf1cb3b80759131c73f6a9dddebccac039dea Author: Russell Bryant <rbryant@redhat.com> Date: Fri Jan 31 14:36:32 2025 -0500 Check for SPDX headers using pre-commit Signed-off-by: Russell Bryant <rbryant@redhat.com> --------- Signed-off-by: Russell Bryant <rbryant@redhat.com>
63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
import asyncio
|
|
import itertools
|
|
|
|
import aiohttp
|
|
from aiohttp import web
|
|
|
|
|
|
class RoundRobinProxy:
|
|
|
|
def __init__(self, target_ports):
|
|
self.target_ports = target_ports
|
|
self.port_cycle = itertools.cycle(self.target_ports)
|
|
|
|
async def handle_request(self, request):
|
|
target_port = next(self.port_cycle)
|
|
target_url = f"http://localhost:{target_port}{request.path_qs}"
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
try:
|
|
# Forward the request
|
|
async with session.request(
|
|
method=request.method,
|
|
url=target_url,
|
|
headers=request.headers,
|
|
data=request.content,
|
|
) as response:
|
|
# Start sending the response
|
|
resp = web.StreamResponse(status=response.status,
|
|
headers=response.headers)
|
|
await resp.prepare(request)
|
|
|
|
# Stream the response content
|
|
async for chunk in response.content.iter_any():
|
|
await resp.write(chunk)
|
|
|
|
await resp.write_eof()
|
|
return resp
|
|
|
|
except Exception as e:
|
|
return web.Response(text=f"Error: {str(e)}", status=500)
|
|
|
|
|
|
async def main():
|
|
proxy = RoundRobinProxy([8100, 8200])
|
|
app = web.Application()
|
|
app.router.add_route('*', '/{path:.*}', proxy.handle_request)
|
|
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, 'localhost', 8000)
|
|
await site.start()
|
|
|
|
print("Proxy server started on http://localhost:8000")
|
|
|
|
# Keep the server running
|
|
await asyncio.Event().wait()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
asyncio.run(main())
|