Looking for example of how to via MicroPython set a 'get' request and received the returned results? #15968
Replies: 3 comments
-
You have to use the urequests library. https://makeblock-micropython-api.readthedocs.io/en/latest/public_library/Third-party-libraries/urequests.html |
Beta Was this translation helpful? Give feedback.
-
You can try this simple script: import socket, network
# connect to WiFi
ssid, pwd = 'yourssid', 'yourpwd'
network.WLAN(network.AP_IF).active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, pwd)
# server def
srv = '192.168.4.85' # your http server
port = 5555 # http port
addr = socket.getaddrinfo(srv, port)[0][-1]
reply = ''
def curl():
global addr
s = socket.socket()
try:
s.connect(addr)
except Exception as e:
print('========================')
print('Connect exception:', e)
print('========================')
s.close()
del s
return False
else:
return request(s)
def request(s):
global srv,port
get = (
"GET /tally/preview HTTP/1.1\r\n"
f"Host: {srv}:{port}\r\n"
"Authorization: Me\r\n"
"Connection: close\r\n"
"\r\n"
)
try:
s.sendall(get.encode())
except Exception as e:
print('------------------------')
print('Send exception:', e)
print('------------------------')
s.close()
del s
return False
else:
return get_reply(s)
def get_reply(s):
global reply
ok = True
response = b""
try:
while True:
data = s.recv(4096)
if not data:
break
response += data
reply = response.decode()
except Exception as e:
print('------------------------')
print('Receive exception:', e)
print('------------------------')
ok = False
s.close()
del s
return ok
ok = curl()
if ok: print(reply) |
Beta Was this translation helpful? Give feedback.
-
Trying to use urequests module, how do you structure the parameters to pass authentication which is just a password? The curl example works... but it is what I could call a non-standard authentication header, no?
The following fails...
Above returns...
I have not tried the raw socket example, as yet, wanted to try to urequests. |
Beta Was this translation helpful? Give feedback.
-
Looking for example of how to via MicroPython set a 'get' request and received the returned results? Basically doing the comparable via curl would be:
An example result would be...
I happen to plan to use an ESP32 module with micropython install, not that I believe that should make any difference. Of course I will have to enable WiFi and have a valid connection to the applicable network.
Beta Was this translation helpful? Give feedback.
All reactions