Home

Awesome

MicroWebCli is a micro HTTP Web client for MicroPython (used on ESP32 and Pycom modules)

HC²

Very easy to integrate and very light with only one file :

Simple but effective :

Using microWebCli fast methods (statics, without classes) :

NameFunction
Makes a GET requestMicroWebCli.GETRequest(url, queryParams=None, auth=None, connTimeoutSec=None)
Makes a POST requestMicroWebCli.POSTRequest(url, formData={}, auth=None, connTimeoutSec=None)
Makes a JSON GET/POST requestMicroWebCli.JSONRequest(url, o=None, auth=None, connTimeoutSec=None)
Save response of a GET request to fileMicroWebCli.FileRequest(url, filepath, progressCallback=None, auth=None, connTimeoutSec=None)

Note : All fast methods automatically bounces on HTTP(S) redirections

Warning : Entire content of the response is allocated in memory with GETRequest, POSTRequest and JSONRequest

Fast methods usage example :

from microWebCli import MicroWebCli

contentBytes = MicroWebCli.GETRequest('http://my-web-site.io/test.html', { 'pet':'cat' })
print(contentBytes)

contentBytes = MicroWebCli.POSTRequest('http://my-web-site.io/post.php', { 'id':12345 })
print(contentBytes)

auth   = MicroWebCli.AuthBasic('toto', 'blah123')
result = MicroWebCli.JSONRequest('http://my-web-site.io/my-api/user/12', auth=auth)
print('User name is %s %s' % (result.firstname, result.lastname))

def progressCallback(microWebCli, progressSize, totalSize) :
  if totalSize :
    print('Progress: %d bytes of %d downloaded...' % (progressSize, totalSize))
  else :
    print('Progress: %d bytes downloaded...' % progressSize)
filename    = '/flash/test.pdf'
contentType = MicroWebCli.FileRequest('http://my-web-site.io/test.pdf', filename, progressCallback)
print('File of content type "%s" was saved to "%s"' % (contentType, filename))

Using microWebCli authentication classes :

AuthenticationClass constructor
Basic HTTPauth = MicroWebCli.AuthBasic(user, password)
Bearer Tokenauth = MicroWebCli.AuthToken(token)

Using microWebCli main class :

NameFunction
ConstructorwCli = MicroWebCli(url='', method='GET', auth=None, connTimeoutSec=None)
Opens HTTP requestwCli.OpenRequest(data=None, contentType=None, contentLength=None)
Opens HTTP form-urlencoded requestwCli.OpenRequestFormData(formData={})
Opens HTTP json object requestwCli.OpenRequestJSONData(o=None)
Writes binary data to opened requestwCli.RequestWriteData(data)
Gets response class for opened requestwCli.GetResponse()
Checks if no request is openedwCli.IsClosed()
Closes an opened requestwCli.Close()
NameProperty
Gets or sets the connection timeout in secondeswCli.ConnTimeoutSec
Gets or sets the Web method verbwCli.Method
Gets or sets the complete Web URLwCli.URL
Gets or sets the Web proto (http/https)wCli.Proto
Gets or sets the Web hostwCli.Host
Gets or sets the Web port numberwCli.Port
Gets or sets the Web path partwCli.Path
Gets or sets the Web complete query stringwCli.QueryString
Gets or sets the dict query parameterswCli.QueryParams
Gets or sets the dict headerswCli.Headers
Gets or sets the authentication classwCli.Auth
Gets or sets a SOCKS5 server (tuple of host and port)wCli.Socks5Addr

Using microWebCli response class :

NameFunction
Gets the client microWebCli classresp.GetClient()
Gets the HTTP server addressresp.GetAddr()
Gets the HTTP server IP addressresp.GetIPAddr()
Gets the HTTP server port numberresp.GetPort()
Gets the HTTP server versionresp.GetHTTPVersion()
Gets the response status coderesp.GetStatusCode()
Gets the response status messageresp.GetStatusMessage()
Checks if it is a success responseresp.IsSuccess()
Checks if it is a location moved responseresp.IsLocationMoved()
Gets the location moved URLresp.LocationMovedURL()
Gets the dict response headersresp.GetHeaders()
Gets the reponse content typeresp.GetContentType()
Gets the response content lengthresp.GetContentLength()
Reads response content (part or total)resp.ReadContent(size=None)
Reads response content into bufferresp.ReadContentInto(buf, nbytes=None)()
Reads response content as json objectresp.ReadContentAsJSON()
Writes response content to a fileresp.WriteContentToFile(filepath, progressCallback=None)
Checks if response is closedresp.IsClosed()
Closes responseresp.Close()

Example of GET (large response) :

from microWebCli import MicroWebCli

wCli = MicroWebCli('http://my-web-site.io/test.html')
wCli.QueryParams['pet'] = 'cat'
print('GET %s' % wCli.URL)
wCli.OpenRequest()
buf  = memoryview(bytearray(1024))
resp = wCli.GetResponse()
if resp.IsSuccess() :
  while not resp.IsClosed() :
    x = resp.ReadContentInto(buf)
    if x < len(buf) :
      buf = buf[:x]
    print(buf.decode())
  print('GET success with "%s" content type' % resp.GetContentType())
else :
  print('GET return %d code (%s)' % (resp.GetStatusCode(), resp.GetStatusMessage()))

Example of PUT (large request) :

from microWebCli import MicroWebCli

auth = MicroWebCli.AuthToken('Dk23JHsI7NsMcY3C=')
wCli = MicroWebCli('http://my-web-site.io/my-api/user/12', 'PUT', auth)
print('PUT %s' % wCli.URL)
wCli.OpenRequest('application/octet-stream', fileSize)
while True :
  data = fileObject.read(1024)
  if not data :
    break
  wCli.RequestWriteData(data)
resp = wCli.GetResponse()
if resp.IsSuccess() :
  o = resp.ReadContentAsJSON()
  print('PUT success')
else :
  print('PUT return %d code (%s)' % (resp.GetStatusCode(), resp.GetStatusMessage()))

Example of saving response to a file :

from microWebCli import MicroWebCli

def progressCallback(microWebCli, progressSize, totalSize) :
  if totalSize :
    print('Progress: %d bytes of %d downloaded...' % (progressSize, totalSize))
  else :
    print('Progress: %d bytes downloaded...' % progressSize)

filename = '/flash/test.pdf'
wCli = MicroWebCli('http://my-web-site.io/test.pdf')
print('GET file %s' % wCli.URL)
wCli.OpenRequest()
resp = wCli.GetResponse()
contentType = resp.WriteContentToFile(filename, progressCallback)
print('File of content type "%s" was saved to "%s"' % (contentType, filename))

By JC`zic for HC² ;')

Keep it simple, stupid :+1: