evaluate "except OSError as er:"

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

evaluate "except OSError as er:"

Post by polygontwist » Sat Jan 10, 2015 10:57 pm

v1.3.8-43-gc114496
code from SD-Card. using WiFi CC3K.

How ca i find the OSError-Codes?
And how can I evaluate them?

i write a little WiFi-Server, i have Error: 57, 32, -57 or 1 and do not know what they mean.

Code: Select all

except OSError as er:
  print ("	OSError",er) 
"er" is not String and not integer, an Objekt?

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

Re: evaluate "except OSError as er:"

Post by Damien » Sun Jan 11, 2015 3:38 pm

"er" is an exception object, and to get the value/argument of this you can use "er.args", or "er.args[0]" to get the first argument (which should be the errno in this case).

Which line of code raises the error?

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: evaluate "except OSError as er:"

Post by polygontwist » Sun Jan 11, 2015 5:23 pm

Thank You. The one with the object I've been thinking, only found no documentation.

The Error is in this Line:

Code: Select all

csock, caddr = my_socket.accept()
i have upload the projekt to:
https://github.com/polygontwist/HTTPServer

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

Re: evaluate "except OSError as er:"

Post by Damien » Wed Jan 14, 2015 10:02 pm

polygontwist wrote:Thank You. The one with the object I've been thinking, only found no documentation.
Well, that in particular is just how normal Python behaves :)
The Error is in this Line:

Code: Select all

csock, caddr = my_socket.accept()
Ok, so it can raise an error if the socket is "in progress", or if there is a general error. That's not very helpful, unfortunately...

Here is my test TCP server and client code that works on my Adafruit CC3000 board (and also on my PC):

Code: Select all

import sys

if sys.platform == 'pyboard':
    import pyb
    import network

    print('using CC3K driver')
    print('creating')
    nic = network.CC3K(pyb.SPI(2), pyb.Pin('Y5'), pyb.Pin('Y4'), pyb.Pin('Y3'))
    print('created')
    nic.connect('ssid', 'password')
    print('connecting')
    while not nic.isconnected():
        pyb.delay(50)
    print('connected')

    print(nic.ifconfig())

def tcp_client():
    import socket
    addr = socket.getaddrinfo('micropython.org', 80)
    print(addr)
    for i in range(10):
        s = socket.socket()
        print(s)
        print(s.connect(addr[0][-1]))
        print(s.send(b'GET http://micropython.org/ks/test.html HTTP/1.0\r\n\r\n'))
        while True:
            data = s.recv(100)
            if data:
                print(len(data), data)
            else:
                break
        s.close()
        print('done')

def tcp_server():
    import socket, time
    s = socket.socket()
    port = 8080
    s.bind(('', port))
    s.listen(5)
    for i in range(10):
        print("waiting for connection on port %d..." % port)
        cl, addr = s.accept()
        print(cl, addr)
        data = cl.recv(20)
        print(len(data), data)
        cl.send(b'here is some data\r\nback for you!\r\n')
        print('sent')
        time.sleep(2)
        cl.close()
    s.close()

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: evaluate "except OSError as er:"

Post by polygontwist » Sat Jan 17, 2015 6:31 pm

ok, i have rewrite my code. In principle, it is the same as your example.
I have in Line
data=csock.recv(128)
nevertheless the error "OSError -57" - after 4 connections
next Time in Line

Code: Select all

csock, caddr = my_socket.accept()
"OSError: 57"
...

I hope you could improve the code. Otherwise, I must try yourself to communicate with the CC3k.

thanks

Post Reply