Skip to content

Commit 9456e93

Browse files
authored
More fixes, goal is code quality of 6
1 parent 96aca0f commit 9456e93

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

picorpcserver/httprequest.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import socket
77
from .netmgr import NetMgr
88
class HTTPRequest:
9-
109
raw_msg = ""
1110
hsock = None
1211
nmgr = None
@@ -16,7 +15,7 @@ class HTTPRequest:
1615
method = ""
1716
uri = ""
1817
protocol = ""
19-
18+
2019
def __init__(self,nm,sock):
2120
"""
2221
Initializes the http server, it requires the following parameters
@@ -30,25 +29,25 @@ def __init__(self,nm,sock):
3029
print("HTTPRequest::__init__")
3130
self.raw_msg = nm.read_all(sock)
3231
self._parse()
33-
32+
3433
def socket(self):
3534
"""
3635
Returns the listening socket used to create this object
3736
"""
3837
return self.hsock
39-
38+
4039
def net_manager(self):
4140
"""
4241
Returns the connected NetMgr object used to construct the object
4342
"""
4443
return self.nmgr
45-
44+
4645
def raw_message(self):
4746
"""
4847
Returns the raw message read from the socket to process
4948
"""
5049
return self.raw_msg
51-
50+
5251
def _parse(self):
5352
"""
5453
Parses the message filling in the method, url, headers, and raw message sent.
@@ -79,7 +78,7 @@ def _parse(self):
7978
if self.verbose:
8079
print(self.headers)
8180
return True
82-
81+
8382
def method(self):
8483
"""
8584
The method used for this request, usually POST or GET
@@ -103,14 +102,14 @@ def payload(self):
103102
The raw data sent after the headers. Will be blank if its a get request
104103
"""
105104
return self.payload
106-
105+
107106
def headerval(self,key):
108107
"""
109108
Returns a case insensitive match for a header and retrieve its value
110109
"""
111110
if self.verbose:
112111
print(f"HTTPRequest::headerval ({key})")
113-
112+
114113
cmpkey = key.lower()
115114
for k in self.headers:
116115
if k.lower() == cmpkey:

picorpcserver/netmgr.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import network
1+
"""
2+
The network manager class, manages reads/writes/connections, etc..
3+
It supports static addresses and dhcp, but only supports ipv4
4+
That is due to the underlying micropython methods
5+
"""
26
import socket
37
from time import sleep
48
import machine
9+
import network
510

611
WIFI_TIMEOUT = 10
712

813
class NetMgr:
9-
1014
led = None
1115
wlan = None
1216
ssid = ""
@@ -15,7 +19,7 @@ class NetMgr:
1519
connection = None
1620
PAGE_SIZE = 4096
1721
verbose = False
18-
22+
1923
def __init__(self,ssid,passwd,ifconfig=None,verbose=False):
2024
self.ssid = ssid
2125
self.passw = passwd
@@ -30,17 +34,17 @@ def __init__(self,ssid,passwd,ifconfig=None,verbose=False):
3034
self.wlan.connect(ssid,passwd)
3135
if verbose:
3236
print("NetMgr::__init__")
33-
37+
3438
def __del__(self):
3539
if verbose:
3640
print("NetMgr::__del__")
3741
self.connection.close()
3842
self.wlan.disconnect()
3943
self.led.off()
40-
44+
4145
def is_connected(self):
4246
return self.wlan.isconnected()
43-
47+
4448
def if_config(self):
4549
return self.wlan.ifconfig()
4650

@@ -64,7 +68,7 @@ def wait_for_connected(self):
6468
self.net_fail()
6569

6670
return self.wlan.isconnected()
67-
71+
6872
def bind_to_port(self,port):
6973
if self.verbose:
7074
print(f"NetMgr::bind_to_port({port})")
@@ -94,7 +98,7 @@ def next_request(self):
9498
if self.verbose:
9599
print(f"NetMgr::next_request Connection from {acc[1]}")
96100
return acc[0]
97-
101+
98102
def read_all(self,sock):
99103
rv = ""
100104
inbytes = sock.recv(NetMgr.PAGE_SIZE)
@@ -105,7 +109,7 @@ def read_all(self,sock):
105109
if self.verbose:
106110
print(f"NetMgr::read_all Read {len(rv)} bytes")
107111
return rv
108-
112+
109113
def read(self,sock,sz):
110114
if sz == -1:
111115
return self.read_all(sock)
@@ -118,7 +122,7 @@ def read(self,sock,sz):
118122
if self.verbose:
119123
print(f"NetMgr::read Read {len(rv)} bytes")
120124
return rv
121-
125+
122126
def write(self,sock: socket.socket,data: bytes):
123127
sz = len(data)
124128
sent = sock.write(data)
@@ -127,7 +131,7 @@ def write(self,sock: socket.socket,data: bytes):
127131
if self.verbose:
128132
print(f"NetMgr::write Wrote {sent} bytes")
129133
return sent
130-
134+
131135
def close(self):
132136
if self.verbose:
133137
print("NetMgr::close")
@@ -136,7 +140,7 @@ def close(self):
136140
self.connection = None
137141
self.wlan.disconnect()
138142
self.led.off()
139-
143+
140144
def net_fail(self):
141145
while ( 1 == 1 ):
142146
sleep(0.25)

0 commit comments

Comments
 (0)