implemented deck.http
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# High-level HTTP protocol interaction
|
||||
# Created by Luxferre in 2025, released into public domain
|
||||
|
||||
# init requests library in a platform-agnostic way
|
||||
requests = None
|
||||
try:
|
||||
from deck import net
|
||||
status, stext, myip = net.wifi_connect_wait()
|
||||
if status:
|
||||
requests = net.init_requests()
|
||||
except:
|
||||
import requests
|
||||
|
||||
DEFAULT_UA = 'Mozilla/5.0'
|
||||
def http_set_ua(ua_str='Mozilla/5.0'):
|
||||
"""Set a default user agent"""
|
||||
global DEFAULT_UA
|
||||
DEFAULT_UA = ua_str
|
||||
|
||||
def http_fetch(url, useragent=None, hdrs={}):
|
||||
"""Fetch text content from URL"""
|
||||
if useragent is None:
|
||||
useragent = DEFAULT_UA
|
||||
hdrs['User-Agent'] = useragent
|
||||
try:
|
||||
r = requests.get(url, headers=hdrs)
|
||||
return (r.status_code, r.text)
|
||||
except:
|
||||
return (404, '')
|
||||
|
||||
def wget(url, filename='', useragent=None, hdrs={}):
|
||||
"""Download a file"""
|
||||
if filename == '':
|
||||
filename = url.split('/')[-1]
|
||||
if useragent is None:
|
||||
useragent = DEFAULT_UA
|
||||
hdrs['User-Agent'] = useragent
|
||||
r = requests.get(url, stream=True, headers=hdrs)
|
||||
if r.status_code >= 200 and r.status_code < 400:
|
||||
with open(filename, 'wb') as f:
|
||||
try:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
except AttributeError:
|
||||
f.write(r.content)
|
||||
else: filename = ''
|
||||
return (r.status_code, filename)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user