websocket client and server

This commit is contained in:
2021-01-13 00:42:36 +01:00
parent fe4bbb9efb
commit b2669f6c65
3 changed files with 69 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env python
import asyncio
import websockets
async def hello(uri):
async with websockets.connect(uri) as websocket:
await websocket.send("Hello world!")
await websocket.recv()
asyncio.get_event_loop().run_until_complete(
hello('ws://localhost:8765'))
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env python
import asyncio
import websockets
async def echo(websocket, path):
async for message in websocket:
await websocket.send(message)
asyncio.get_event_loop().run_until_complete(
websockets.serve(echo, 'localhost', 8765))
asyncio.get_event_loop().run_forever()
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/python
import base64
import requests
def get_screenshot(params):
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
"userkey": "HQKRAKBKRARAAJBKYEIAAQ"}
try:
r = requests.post('https://api.site-shot.com/', headers=headers, data=params)
if (r.status_code == requests.codes.ok):
return r.json()
elif (r.status_code == 404):
print("Screenshot hasn't been generated. The error: " + r.json().error)
elif (r.status_code == 401):
print("Invalid authentication token")
elif (r.status_code == 403):
print("Active subscription hasn't been found")
except requests.exceptions.RequestException as e:
print('Screenshot generation has failed, the error: ' + str(e))
def main():
screenshot = get_screenshot(
{'url': 'https://site-shot.com/',
'width': 1280,
'height': 1280,
'format': 'png',
'response_type': 'json',
'delay_time': 2000,
'timeout': 60000})
if screenshot is not None:
base64_image = screenshot['image'].split(',', maxsplit=1)[1]
image_file = open('screenshot.png', 'wb')
image_file.write(base64.b64decode(base64_image))
image_file.close()
main()