Skip to content

Commit b141933

Browse files
committed
Perver webserver fix - BEWARE, P027 input change
1 parent 512d8c2 commit b141933

File tree

4 files changed

+41
-50
lines changed

4 files changed

+41
-50
lines changed

RPIEasy.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def hardwareInit():
5757
if rpieGlobals.osinuse=="linux":
5858
import linux_os as OS
5959
import linux_network as Network
60-
if OS.isAlreadyRunning()>1:
61-
time.sleep(1)
62-
print("\nAn RPIEasy is already running! Close that first!")
63-
sys.exit(0)
60+
# if OS.isAlreadyRunning()>1:
61+
# time.sleep(1)
62+
# print("\nAn RPIEasy is already running! Close that first!")
63+
# sys.exit(0)
6464
Settings.NetMan = Network.NetworkManager()
6565
if len(OS.getsounddevs())>0:
6666
Settings.SoundSystem["usable"]=True

_P027_INA219.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def webform_load(self): # create html page for settings
121121
webserver.addFormSelector("Max voltage","plugin_027_volt",len(options),options,optionvalues,None,int(choice3))
122122
webserver.addUnit("V")
123123

124-
webserver.addFormFloatNumberBox("Shunt resistor","plugin_027_shuntohm",self.shuntohms,0,30000000.0)
124+
webserver.addFormTextBox("Shunt resistor","plugin_027_shuntohm",self.shuntohms,12)
125125
webserver.addUnit("Ohm")
126126
if self.ina is not None:
127127
choice7 = self.taskdevicepluginconfig[6]

perver.py

+35-44
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
# Version control:
4343
__author__ = 'SweetPalma'
44-
__version__ = '0.25'
44+
__version__ = '0.30'
4545

4646

4747
# Custom internal exceptions:
@@ -61,8 +61,7 @@ def __init__(self, server):
6161
self.server = server
6262

6363
# Handling requests:
64-
@asyncio.coroutine
65-
def handle_request(self, reader, writer):
64+
async def handle_request(self, reader, writer):
6665

6766
# Preparing basic values:
6867
peername = writer.get_extra_info('peername')
@@ -93,7 +92,7 @@ def handle_request(self, reader, writer):
9392
try:
9493

9594
# Reading:
96-
line = yield from reader.readline()
95+
line = await reader.readline()
9796

9897
# Setting request type and maximal request size at start:
9998
if len(header) == 0:
@@ -122,15 +121,15 @@ def handle_request(self, reader, writer):
122121
# Reading content:
123122
content = b''
124123
if 0 < length < request_max:
125-
content = yield from reader.readexactly(length)
124+
content = await reader.readexactly(length)
126125

127126
# Close connection in case of big file:
128127
elif length > request_max:
129128
self.writer.close()
130129
raise killer('REQUEST IS TOO BIG')
131130

132131
# Parsing data:
133-
self.client = yield from self.build_client(header, content)
132+
self.client = await self.build_client(header, content)
134133
client = self.client
135134

136135
# In case of disconnection:
@@ -148,70 +147,66 @@ def handle_request(self, reader, writer):
148147
route_post = self.check_route(client.path, self.server.route_post)
149148
route_get = self.check_route(client.path, self.server.route_get)
150149
if client.type == 'POST' and route_post:
151-
raise killer((yield from self.respond_script(*route_post)))
150+
raise killer((await self.respond_script(*route_post)))
152151
if client.type == 'GET' and route_get:
153-
raise killer((yield from self.respond_script(*route_get)))
152+
raise killer((await self.respond_script(*route_get)))
154153

155154
# Checking static files:
156155
for dir, real in self.server.route_static.items():
157156
if client.path.startswith(dir):
158157
filepath = client.path.replace(dir, real, 1)
159-
raise killer((yield from self.respond_file(filepath[1:])))
158+
raise killer((await self.respond_file(filepath[1:])))
160159

161160
# Routing 404 error:
162-
raise killer((yield from self.respond_error(404)))
161+
raise killer((await self.respond_error(404)))
163162

164163
# Timeout/Cancelled:
165164
except concurrent.futures._base.CancelledError:
166-
yield from self.respond_error(500)
165+
await self.respond_error(500)
167166
log.info(client_info + ' TIMED OUT')
168167

169168
# Terminator:
170169
except killer as exception:
171170
log.info(client_info + ' ' + exception.message)
172171

173172
# Sending file:
174-
@asyncio.coroutine
175-
def respond_file(self, path):
173+
async def respond_file(self, path):
176174
try:
177175
with open(path, "rb") as file:
178176
size = os.path.getsize(path)
179-
return (yield from self.respond(
177+
return (await self.respond(
180178
status = 200,
181179
content = file.read(),
182180
type = self.get_mime(path),
183181
length = size
184182
))
185183
# No file found:
186184
except IOError:
187-
return (yield from self.respond_error(404))
185+
return (await self.respond_error(404))
188186

189187
# Sending error message:
190-
@asyncio.coroutine
191-
def respond_error(self, number, custom=None):
188+
async def respond_error(self, number, custom=None):
192189
error = {
193190
400: 'Bad Request',
194191
404: 'Not Found',
195192
500: 'Internal Error',
196193
}
197194
error_text = number in error and error[number] or 'Unknown Error'
198195
error_cont = str(number) + ' ' + error_text
199-
return (yield from self.respond(number, error_cont))
196+
return (await self.respond(number, error_cont))
200197

201198
# Executing client script and sending it response:
202-
@asyncio.coroutine
203-
def respond_script(self, script, keys={}):
204-
script_result = (yield from script(self.client, **keys)) or b''
205-
return (yield from self.respond(
199+
async def respond_script(self, script, keys={}):
200+
script_result = (await script(self.client, **keys)) or b''
201+
return (await self.respond(
206202
status = self.client.status,
207203
content = script_result,
208204
header = self.client.header,
209205
type = self.client.mime
210206
))
211207

212208
# Pure data response:
213-
@asyncio.coroutine
214-
def respond(self, status, content=b'', type='text/html', length=None, header={}):
209+
async def respond(self, status, content=b'', type='text/html', length=None, header={}):
215210

216211
# Forming header:
217212
encoding = self.server.encoding
@@ -302,8 +297,7 @@ def get_mime(self, path):
302297
return guess_type(path)[0] or 'application'
303298

304299
# Parsing GET and COOKIES:
305-
@asyncio.coroutine
306-
def parse(self, path):
300+
async def parse(self, path):
307301
# Preparing %key%=%value% regex:
308302
try:
309303
get_word = '[^=;&?]'
@@ -323,8 +317,7 @@ def parse(self, path):
323317
return dict(matched)
324318

325319
# Parsing POST multipart:
326-
@asyncio.coroutine
327-
def parse_post(self, content, types, boundary):
320+
async def parse_post(self, content, types, boundary):
328321

329322
# Establishing default encoding:
330323
encoding = self.server.encoding
@@ -376,11 +369,10 @@ def parse_post(self, content, types, boundary):
376369
else:
377370
if isinstance(content, bytes):
378371
content = content.decode(encoding)
379-
return self.parse(content)
372+
return await self.parse(content)
380373

381374
# Parsing client data:
382-
@asyncio.coroutine
383-
def build_client(self, header_raw, content_raw=b''):
375+
async def build_client(self, header_raw, content_raw=b''):
384376

385377
# Safe dict values:
386378
def safe_dict(dictionary, value, default):
@@ -441,9 +433,9 @@ def safe_dict(dictionary, value, default):
441433
boundary = b''
442434

443435
# POST/GET/COOKIES:
444-
client.get = yield from self.parse(GET)
445-
client.post = yield from self.parse_post(content_raw, client.form_type, boundary)
446-
client.cookie = yield from self.parse(safe_dict(header, 'Cookie', ''))
436+
client.get = await self.parse(GET)
437+
client.post = await self.parse_post(content_raw, client.form_type, boundary)
438+
client.cookie = await self.parse(safe_dict(header, 'Cookie', ''))
447439

448440
# Client ID cookie, can be overrided later:
449441
client.header['Set-Cookie'] = 'id=' + client.id
@@ -463,7 +455,7 @@ def safe_dict(dictionary, value, default):
463455
# In case of fail:
464456
except BaseException as exc:
465457
log.warning('Error parsing user request.')
466-
yield from self.respond_error(400)
458+
await self.respond_error(400)
467459
raise exc
468460

469461
# Script client:
@@ -596,8 +588,8 @@ def get(self, path):
596588
""" Binds all GET requests from path to certain function. """
597589
def decorator(func):
598590
@wraps(func)
599-
def wrapper(*args, **kwds):
600-
return asyncio.coroutine(func)(*args, **kwds)
591+
async def wrapper(*args, **kwds):
592+
return func(*args, **kwds)
601593
self.route_get[path] = wrapper
602594
return wrapper
603595
return decorator
@@ -608,8 +600,8 @@ def post(self, path):
608600
""" Binds all POST requests from path to certain function. """
609601
def decorator(func):
610602
@wraps(func)
611-
def wrapper(*args, **kwds):
612-
return asyncio.coroutine(func)(*args, **kwds)
603+
async def wrapper(*args, **kwds):
604+
return func(*args, **kwds)
613605
self.route_post[path] = wrapper
614606
return wrapper
615607
return decorator
@@ -620,8 +612,8 @@ def route(self, path):
620612
""" Binds all POST/GET requests from path to certain function. """
621613
def decorator(func):
622614
@wraps(func)
623-
def wrapper(*args, **kwds):
624-
return asyncio.coroutine(func)(*args, **kwds)
615+
async def wrapper(*args, **kwds):
616+
return func(*args, **kwds)
625617
self.route_post[path] = wrapper
626618
self.route_get[path] = wrapper
627619
return wrapper
@@ -676,11 +668,10 @@ def stop(self):
676668
self._loop.stop()
677669

678670
# HTTP request handler:
679-
@asyncio.coroutine
680-
def handler(self, reader, writer):
671+
async def handler(self, reader, writer):
681672
try:
682673
handler = PerverHandler(self)
683-
yield from asyncio.wait_for(
674+
await asyncio.wait_for(
684675
handler.handle_request(reader, writer),
685676
timeout=self.timeout
686677
)

rpieGlobals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Copyright (C) 2018-2023 by Alexander Nagy - https://bitekmindenhol.blog.hu/
77
#
88
PROGNAME = "RPIEasy"
9-
BUILD = 23101
9+
BUILD = 23149
1010
PROGVER = str(BUILD)[:1]+"."+str(BUILD)[1:2]+"."+str(BUILD)[2:]
1111

1212
gpMenu = []

0 commit comments

Comments
 (0)