forked from TEN-framework/ten-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.py
More file actions
151 lines (114 loc) · 4.01 KB
/
Copy pathhttp.py
File metadata and controls
151 lines (114 loc) · 4.01 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
# Copyright © 2025 Agora
# This file is part of TEN Framework, an open source project.
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
from http.client import RemoteDisconnected
import json
import ssl
import socket
import time
from urllib import request, error
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
def send_request_with_body(uri: str, body, method: str):
body = json.dumps(body).encode("utf8")
header = {"Content-Type": "application/json"}
req = request.Request(url=uri, data=body, headers=header, method=method)
try:
# Ignore the proxy settings in the environment.
proxy_handler = request.ProxyHandler({})
https_handler = request.HTTPSHandler(context=ctx)
opener = request.build_opener(proxy_handler, https_handler)
with opener.open(req) as stream:
res = stream.read().decode("utf8")
return res
except error.HTTPError as e:
return e.code
except Exception as e:
return str(e)
def post(uri: str, body):
return send_request_with_body(uri, body, "POST")
def put(uri: str, body):
return send_request_with_body(uri, body, "PUT")
def patch(uri: str, body):
return send_request_with_body(uri, body, "PATCH")
def delete(uri: str):
req = request.Request(url=uri, method="DELETE")
try:
# Ignore the proxy settings in the environment.
proxy_handler = request.ProxyHandler({})
https_handler = request.HTTPSHandler(context=ctx)
opener = request.build_opener(proxy_handler, https_handler)
with opener.open(req) as stream:
res = stream.read().decode("utf8")
return res
except error.HTTPError as e:
return e.code
except Exception as e:
return str(e)
def get(uri: str):
try:
# Ignore the proxy settings in the environment.
proxy_handler = request.ProxyHandler({})
https_handler = request.HTTPSHandler(context=ctx)
opener = request.build_opener(proxy_handler, https_handler)
with opener.open(uri) as stream:
data = stream.read()
return data.decode("utf8")
except error.HTTPError as e:
return e.code
except Exception as e:
return str(e)
def detect_port(ip: str, port: int) -> bool:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except Exception:
return False
def is_app_started(ip: str, port: int, timeout=10) -> bool:
duration = 0
is_started = False
while duration < timeout:
is_started = detect_port(ip, port)
if is_started:
break
duration += 1
time.sleep(1)
return is_started
def is_app_stopped(ip: str, port: int, timeout=10) -> bool:
duration = 0
is_running = True
while duration < timeout:
is_running = detect_port(ip, port)
if not is_running:
break
duration += 1
time.sleep(1)
if not is_running:
print("The TEN app stops in %d seconds" % (duration))
return not is_running
def stop_app(ip: str, port: int, timeout=10, is_https=False) -> bool:
schema = "http"
if is_https:
schema = "https"
uri = "%s://%s:%d/" % (schema, ip, port)
body = json.dumps({"ten": {"type": "close_app"}}).encode("utf8")
header = {"Content-Type": "application/json"}
req = request.Request(url=uri, data=body, headers=header, method="POST")
try:
# Ignore the proxy settings in the environment.
proxy_handler = request.ProxyHandler({})
https_handler = request.HTTPSHandler(context=ctx)
opener = request.build_opener(proxy_handler, https_handler)
handler = opener.open(req)
handler.close()
except RemoteDisconnected:
pass
except error.HTTPError:
pass
return is_app_stopped(ip, port, timeout)