-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
import requests | ||
import websockets | ||
|
||
def send_http_request(url, method, headers=None, data=None): | ||
""" | ||
Sends an HTTP request to the specified URL using the specified method, headers, and data. | ||
""" | ||
if headers is None: | ||
headers = {} | ||
|
||
if method == "GET": | ||
response = requests.get(url, headers=headers, data=data) | ||
elif method == "POST": | ||
response = requests.post(url, headers=headers, data=json.dumps(data)) | ||
elif method == "PUT": | ||
response = requests.put(url, headers=headers, data=json.dumps(data)) | ||
elif method == "DELETE": | ||
response = requests.delete(url, headers=headers, data=data) | ||
else: | ||
raise ValueError("Invalid HTTP method") | ||
|
||
if response.status_code != 200: | ||
raise Exception("HTTP request failed with status code {}".format(response.status_code)) | ||
|
||
return response.json() | ||
|
||
def send_websocket_message(websocket, message): | ||
""" | ||
Sends a message over a WebSocket connection. | ||
""" | ||
websocket.send(json.dumps(message)) | ||
|
||
def receive_websocket_message(websocket): | ||
""" | ||
Receives a message over a WebSocket connection. | ||
""" | ||
return json.loads(websocket.recv()) | ||
|
||
def create_websocket_connection(url): | ||
""" | ||
Creates a new WebSocket connection to the specified URL. | ||
""" | ||
websocket = websockets.connect(url) | ||
return websocket | ||
|
||
def close_websocket_connection(websocket): | ||
""" | ||
Closes the specified WebSocket connection. | ||
""" | ||
websocket.close() |