mini web server adding socket timeout


#1

Since I want to listen for the socket for only 10 seconds I added, following the documentation,

sendtimeout(timeout)

Set a timeout on blocking socket operations. The timeout argument can be a nonnegative integer number expressing milliseconds, or None. If a non-zero value is given, subsequent socket operations will raise a timeout exception if the timeout period value has elapsed before the operation has completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket is put in blocking mode.

this line of code:
<pre class="CodeBlock">sock.sendtimeout(10000)

(as you can see in bold in the basic example code below), but it give me an error at execution time(attribute
error). I noticed that if I write sock. I get some suggestion from the compiler and between these there
isn’t sendtimeout.

<pre class="CodeBlock">
###############################################################################
# Mini Web Server
#
# Created by VIPER Team 2015 CC
# Authors: G. Baldi, D. Mazzei
###############################################################################

# import streams &amp; socket
import streams
import socket

# import the cc3000 wifi driver
from drivers.wifi.cc3000 import cc3000
# import the wifi interface
from wireless import wifi

streams.serial()

# init the cc3000 drivers: works on arduino compatibles and Particle Core.
# The driver automatically registers itself to the wifi interface
# so the wifi module can use the cc3000 without your intervention
cc3000.auto_init()

# use the wifi interface to link to the Access Point
# change network name, security and password as needed
print("Establishing Link...")
try:
    wifi.link("Network-Name",wifi.WIFI_WPA2,"Wifi-Password")
except Exception as e:
    print("ooops, something wrong while linking :(", e)
    while True:
        sleep(1000)

# Yes! we are connected
print("Linked!")

# Let's print the cc3000 ip, it will be needed soon
info = wifi.link_info()
print("My IP is:",info[0])

# Now let's create a socket and listen for incoming connection on port 80
sock = socket.socket()
<b>sock.sendtimout(10000)# Setting a timout a of 10 seconds for the blocking socket</b>
sock.bind(80)
sock.listen()


while True:
    try:
        # Type in your browser the cc3000 ip!
        print("Waiting for connection...")
        # here we wait for a connection
        clientsock,addr = sock.accept()
        print("Incoming connection from",addr)

        # yes! a connection is ready to use
        # first let's create a SocketStream
        # it's like a serial stream, but with a socket underneat.
        # This way we can read and print to the socket
        client = streams.SocketStream(clientsock)

        # let's read all the HTTP headers from the browser
        # stop when a blank line is received
        line = client.readline()
        while line!="\
" and line!="\\r\
":
            line = client.readline()
        print("HTTP request received!")

        # let's now send our headers (very minimal)
        print("HTTP/1.1 200 OK\\r",stream=client)
        print("Content-Type: text/html\\r",stream=client)
        print("Connection: close\\r\
\\r",stream=client)
        # see? as easy as print!
        print("<html><body>Hello Viper!",random(0,100),"</body></html>",stream=client)
        # close connection and go waiting for another one
        client.close()
    except Exception as e:
        print("ooops, something wrong:",e)


```

#2

maybe the problem is the photon wifi driver that doesn’t support sendtimeout on socket…


#3

mmm…I’ll look into it (an updated version of Broadcom wifi drivers is coming pretty soon).

However, is it strictly necessary for your mini webserver to have socket timeouts? Since Zerynth is multithreaded, you could launch a reading and a writing thread without the need to check for timeouts.


#4

Threads are working great for my purpouse. 

I was trying to use wifi.link_info() to check if I was connected to my router (reading the ip and assuming 0.0.0.0 for not connected) but that function (and releated thread) was creating some problems to the execution and I tought it was releated to the threads, but then I removed that thread all other threads started working perfectly.

BTW the updated broadcom wifi driver should have the feature to check the connectivity  so my problem should be fixed easily.




#5

Current version of the standard library wifi module already has is_linked. :wink:


#6

 <3 Wow I didn’t noticed! I’ll test it soon. 


#7

I’m testing the mini web server example without any other mod except for my ssid and password and uncommenting photon driver. (so without the timeout)
When I send a request from my browser it’s like I’m sending 2 or 3 requests as you can see in this video. Is this normal behaviour?


EDIT: furthermore if I send 2 fast request(a fast sequence of the refresh button of the browser) it stops working and can’t receive any more request.


#8

mmm…try to print the lines received and see what your browser is asking for ( I guess it’s the “site” favicon  :D)

As for the other issue, I’ll test it asap.


#9

You were right  :smiley:

For the other issue I would add that the MCU keeps working (I added a thread with a blinking led to test this), but it stops in this state:

"Incoming connection from (192.168.0.5, 61069)
never go back to 
Waiting for connection…
The key to get this bug out is to press refresh then after 1 second press refresh again. If you are too slow or too fast it works correctly.