send HTTP Request ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

send HTTP Request ESP8266

Post by samerou » Sun May 12, 2019 3:24 pm

Hello ,
I'm trying for a while to send http request Via web browser without success and I tried everything ,

Any one have a solution on How to send http Request which is

then Extract specific information from the same link ?

I'm stuck in this phase

Best Regards ,

Samerou

User avatar
jimmo
Posts: 898
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: send HTTP Request ESP8266

Post by jimmo » Tue May 14, 2019 2:36 am

Hi,

Can you post some examples of what you've tried?

There's an example in the docs using the sockets API to do HTTP manually: http://docs.micropython.org/en/latest/e ... k_tcp.html
This is easy because it doesn't require you to copy any additional modules or anything, but you'll have to do a lot more yourself.

Or you can use the http.client lib from micropython-lib: https://github.com/micropython/micropyt ... ttp.client
You'll probably need to use upip to install this on your ESP8266.
See https://docs.micropython.org/en/latest/ ... kages.html

Code: Select all

import upip
upip.install('micropython-http.client')
Then see https://github.com/micropython/micropyt ... _client.py for an example of how to use it.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Tue May 14, 2019 8:59 pm

jimmo wrote:
Tue May 14, 2019 2:36 am
Hi,

Can you post some examples of what you've tried?

There's an example in the docs using the sockets API to do HTTP manually: http://docs.micropython.org/en/latest/e ... k_tcp.html
This is easy because it doesn't require you to copy any additional modules or anything, but you'll have to do a lot more yourself.

Or you can use the http.client lib from micropython-lib: https://github.com/micropython/micropyt ... ttp.client
You'll probably need to use upip to install this on your ESP8266.
See https://docs.micropython.org/en/latest/ ... kages.html

Code: Select all

import upip
upip.install('micropython-http.client')
Then see https://github.com/micropython/micropyt ... _client.py for an example of how to use it.

This the Code that I'm using :

Code: Select all

import http.client
conn = http.client.HTTPConnection("http://192.168.8.100:80/info.php?request = sam")
conn.request("HEAD","/info.php?request = sam")
res = conn.getresponse()
print (res.status, res.reason)
# Result:
print("200 OK")
print("The pastebin URL is:%s"%pastebin_url) 
before using that I tried several time to install the http.client but see below I get an error

Code: Select all

>>> import upip
>>> upip.install('micropython-http.client')
Installing to: /lib/
Installing micropython-http.client 0.5.1 from https://files.pythonhosted.org/packages/00/1e/8dafdf0e9159e29c352fd4caba9208b763601a60b38905b2da31d869e9af/micropython-http.client-0.5.1.tar.gz
Installing micropython-email.parser 0.5.1 from https://files.pythonhosted.org/packages/ee/f3/7cc9c340b62e11d8004ec005fefbfc7eb4efb897f42553839a463ae5e35c/micropython-email.parser-0.5.1.tar.gz
Installing micropython-email.message 0.5.3 from https://files.pythonhosted.org/packages/ab/9c/5954005302f898c82c587581000dcfa433780fdcd51e75b7faf9b29eb575/micropython-email.message-0.5.3.tar.gz
Installing micropython-socket 0.5.3 from https://files.pythonhosted.org/packages/36/ff/0fdb5253fb7ecedc947fd39a7acebecdb3600ce2656c27efb885199a406f/micropython-socket-0.5.3.tar.gz
Installing micropython-collections 0.1.3 from https://files.pythonhosted.org/packages/17/c8/2da83be4fe0cf730fcbf62ac584171d0ce0c4a0dd506b9a77930b0f4191c/micropython-collections-0.1.3.tar.gz
Installing micropython-urllib.parse 0.5.2 from https://files.pythonhosted.org/packages/f8/96/ce237913238103c4f5b4c3f3d07165c92cc654668c7be3445adbb9d45967/micropython-urllib.parse-0.5.2.tar.gz
Installing micropython-warnings 0.1.1 from https://files.pythonhosted.org/packages/73/61/246c5fe2b809c3598f3c66cea4cde1ee71c6c4eba806781e94efe8bb7729/micropython-warnings-0.1.1.tar.gz
Installing micropython-email.feedparser 0.5.1 from https://files.pythonhosted.org/packages/43/0c/be9482f266f4d503a8607ee6062d37f325d5bc5bc05ac98b814388eb3405/micropython-email.feedparser-0.5.1.tar.gz
Installing micropython-email.internal 0.5.1 from https://files.pythonhosted.org/packages/5d/25/3f845fcaa8455a55ae7ca0d1f81b6b0c5501c8c878c7ce16cc7126e5b54e/micropython-email.internal-0.5.1.tar.gz
Error installing 'micropython-re-pcre': , packages may be partially installed

I tried also another code which provided at this link :

Code: Select all

def http_get(url):
    _, _, host, path = url.split('/', 3)
    addr = socket.getaddrinfo(host, 80)[0][-1]
    s = socket.socket()
    s.connect(addr)
    s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
    while True:
        data = s.recv(100)
        if data:
            print(str(data, 'utf8'), end='')
        else:
            break
    s.close()
and then executed :

Code: Select all

import socket 
http_get('http://192.168.8.100/info.php?request=samerou')


and as error I got this

Code: Select all

>>> http_get('http://192.168.8.100/info.php?request=samerou')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 5, in http_get
OSError: [Errno 103] ECONNABORTED
Any idea about the two solutions .

Damien
Site Admin
Posts: 623
Joined: Mon Dec 09, 2013 5:02 pm

Re: send HTTP Request ESP8266

Post by Damien » Wed May 15, 2019 6:52 am

To do a HTTP(s) request as a client you can use the urequests module. Install with upip.install('urequests') and then use:

Code: Select all

import urequests
response = urequests.get(URL)
print(response.text)
response.close()
(Make sure to use the latest firmware version.)

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Wed May 15, 2019 8:56 am

Damien wrote:
Wed May 15, 2019 6:52 am
To do a HTTP(s) request as a client you can use the urequests module. Install with upip.install('urequests') and then use:

Code: Select all

import urequests
response = urequests.get(URL)
print(response.text)
response.close()
(Make sure to use the latest firmware version.)
I will try it tonight thank you ,

For installing package I use upip.install('urequests') not upip.install('micropython-urequests') ?
Could you please give me info about your latest firmware version ?

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Fri May 17, 2019 10:00 am

Damien wrote:
Wed May 15, 2019 6:52 am
To do a HTTP(s) request as a client you can use the urequests module. Install with upip.install('urequests') and then use:

Code: Select all

import urequests
response = urequests.get(URL)
print(response.text)
response.close()
(Make sure to use the latest firmware version.)
Hello Damien ,
I donwload the Package from Micropython-lib/uresquests on Github and installed it on my ESP8266 ,
the

Code: Select all

import urequests

Works fine but the problem when I execute this :

Code: Select all

response = urequests.get(URL)
error :

AttributeError : 'Module' object has no attribute 'get'
the urequests code is below :

Code: Select all

import usocket

class Response:

    def __init__(self, f):
        self.raw = f
        self.encoding = "utf-8"
        self._cached = None

    def close(self):
        if self.raw:
            self.raw.close()
            self.raw = None
        self._cached = None

    @property
    def content(self):
        if self._cached is None:
            try:
                self._cached = self.raw.read()
            finally:
                self.raw.close()
                self.raw = None
        return self._cached

    @property
    def text(self):
        return str(self.content, self.encoding)

    def json(self):
        import ujson
        return ujson.loads(self.content)


def request(method, url, data=None, json=None, headers={}, stream=None):
    try:
        proto, dummy, host, path = url.split("/", 3)
    except ValueError:
        proto, dummy, host = url.split("/", 2)
        path = ""
    if proto == "http:":
        port = 80
    elif proto == "https:":
        import ussl
        port = 443
    else:
        raise ValueError("Unsupported protocol: " + proto)

    if ":" in host:
        host, port = host.split(":", 1)
        port = int(port)

    ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
    ai = ai[0]

    s = usocket.socket(ai[0], ai[1], ai[2])
    try:
        s.connect(ai[-1])
        if proto == "https:":
            s = ussl.wrap_socket(s, server_hostname=host)
        s.write(b"%s /%s HTTP/1.0\r\n" % (method, path))
        if not "Host" in headers:
            s.write(b"Host: %s\r\n" % host)
        # Iterate over keys to avoid tuple alloc
        for k in headers:
            s.write(k)
            s.write(b": ")
            s.write(headers[k])
            s.write(b"\r\n")
        if json is not None:
            assert data is None
            import ujson
            data = ujson.dumps(json)
            s.write(b"Content-Type: application/json\r\n")
        if data:
            s.write(b"Content-Length: %d\r\n" % len(data))
        s.write(b"\r\n")
        if data:
            s.write(data)

        l = s.readline()
        #print(l)
        l = l.split(None, 2)
        status = int(l[1])
        reason = ""
        if len(l) > 2:
            reason = l[2].rstrip()
        while True:
            l = s.readline()
            if not l or l == b"\r\n":
                break
            #print(l)
            if l.startswith(b"Transfer-Encoding:"):
                if b"chunked" in l:
                    raise ValueError("Unsupported " + l)
            elif l.startswith(b"Location:") and not 200 <= status <= 299:
                raise NotImplementedError("Redirects not yet supported")
    except OSError:
        s.close()
        raise

    resp = Response(s)
    resp.status_code = status
    resp.reason = reason
    return resp


def head(url, **kw):
    return request("HEAD", url, **kw)

def get(url, **kw):
    return request("GET", url, **kw)

def post(url, **kw):
    return request("POST", url, **kw)

def put(url, **kw):
    return request("PUT", url, **kw)

def patch(url, **kw):
    return request("PATCH", url, **kw)

def delete(url, **kw):
    return request("DELETE", url, **kw)

AnyIdeas ?

Christian Walther
Posts: 72
Joined: Fri Aug 19, 2016 11:55 am

Re: send HTTP Request ESP8266

Post by Christian Walther » Fri May 17, 2019 9:34 pm

Note that in the stable release esp8266-20190125-v1.10.bin, urequests is built-in, so when you say import urequests, you are getting that one, not the one on the filesystem, unless you changed sys.path. I don’t know why it doesn’t have a get function for you though, it does for me. What does

Code: Select all

help(urequests)
say?

Edit: Could it be that you have an empty module leftover from an earlier failed import attempt? http://docs.micropython.org/en/latest/g ... -as-loaded Try again after a reset.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Fri May 17, 2019 11:32 pm

Christian Walther wrote:
Fri May 17, 2019 9:34 pm
Note that in the stable release esp8266-20190125-v1.10.bin, urequests is built-in, so when you say import urequests, you are getting that one, not the one on the filesystem, unless you changed sys.path. I don’t know why it doesn’t have a get function for you though, it does for me. What does

Code: Select all

help(urequests)
say?

Edit: Could it be that you have an empty module leftover from an earlier failed import attempt? http://docs.micropython.org/en/latest/g ... -as-loaded Try again after a reset.

Thank you for your reply ,

Thank you for your answer , First I tried to reset ESP8266 and Nothing works as usual same errors .

For

Code: Select all

help(urequests)
this the message
https://ibb.co/8m6gqYy

User avatar
jimmo
Posts: 898
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: send HTTP Request ESP8266

Post by jimmo » Sat May 18, 2019 12:39 am

Did you `import urequests` before `help(urequests)` ?

Sorry about the bad advice earlier with http.client. It might be a good idea to completely clear the filesystem on your ESP8266 and make sure it's not getting confused about other libraries on there.

samerou
Posts: 38
Joined: Mon Feb 11, 2019 12:51 pm

Re: send HTTP Request ESP8266

Post by samerou » Sat May 18, 2019 12:46 am

jimmo wrote:
Sat May 18, 2019 12:39 am
Did you `import urequests` before `help(urequests)` ?

Sorry about the bad advice earlier with http.client. It might be a good idea to completely clear the filesystem on your ESP8266 and make sure it's not getting confused about other libraries on there.
I did clear everything inside lib then I inserted the urequests and when I did the

Code: Select all

import urequests 
It worked but the I get the error when I do the get thing

Code: Select all

urequests.get
Edit :

Code: Select all

  help(urequests)    
worked for me see image see link above

Post Reply