forked from imanbeik/EveryNetServer-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientApp.py
38 lines (30 loc) · 1.23 KB
/
ClientApp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import websockets
import asyncio
import requests
import json
ACCESS_TOKEN = None
PORT = None
URI = 'ws://everynetserver.ga:8924'
async def forwarder():
global PORT, URI
async with websockets.connect(URI, extra_headers=[('access_token', ACCESS_TOKEN)]) as websocket:
while True:
full_request_json = await websocket.recv()
full_request = json.loads(full_request_json)
if full_request["method"] == "GET":
ans = requests.get(f"http://localhost{full_request['path']}:{PORT}", headers=full_request['headers'])
else:
ans = requests.post(f"http://localhost{full_request['path']}:{PORT}", headers=full_request['headers'])
response = {}
response['text'] = ans.text
response['headers'] = ans.headers
response_json = json.dumps(response)
await websocket.send(response_json)
if __name__ == "__main__":
PORT = input("Enter port you wanna requests forward to it: ")
ACCESS_TOKEN = input("Enter your access token: ")
try:
asyncio.get_event_loop().run_until_complete(forwarder())
except KeyboardInterrupt:
pass
print("Server stopped")