Go Down

Topic: Like to run python script " arduino.local/sd/myside/my_dynamic_side.py" on YUN (Read 9426 times) previous topic - next topic

OttoF

Hello

i like to build an dynamic webside on yun
i load up it with a scetch like
sketchfolder
            myscetch
            myscetch.ino
                 www
                 my_dynamic_side.py

the first line in my python file is
#!/usr/bin/python

but when i try to test it
arduino.local/sd/myside/my_dynamic_side.py

arduinos webserver ask me to download the script file

what iam doing wrong ?

federicofissore

#1
Oct 30, 2013, 02:33 pm Last Edit: Oct 30, 2013, 03:00 pm by Federico Fissore Reason: 1
Server side code (aka: dynamic web pages) must reside into the cgi-bin folder /www/cgi-bin
There currently is no way to make that page be transferred by the IDE (although that may be a good feature request to put on github)
You'll need to manually copy that to /www/cgi-bin. Don't forget to make your python script cgi aware. Google for python cgi

OttoF

where is this cgi-bin folder ?

below my own www folder from my sketch ?

myscetch
       www
             cgi-bin


federicofissore

Sorry my post got strangely misformatted.
The cgi-bin folder is on the yun, it's /www/cgi-bin. You need to use command line tools like scp, winscp to copy files from your computer to the yun

OttoF

i dont understand this because my statik webside on yun works well

arduino.local/sd/myside/index.html

just when i call

arduino.local/sd/myside/my_dynamic_side.py

i got the answer from yun to download the file.
This means the file is allready placed on yun but the webserver have no idea what is to do with the file.

So i think the yun webserver do not found the python interpreter.


federicofissore

your python script will be run by the webserver IF
- it complies with the CGI spec
- it's located in /www/cgi-bin and therefore served with url http://arduino.local/cgi-bin/my_dynamic_side.py

If you are new to cgi web development, I suggest you first get acquainted with it.
You also wnat to search this forum as someone else has already played with it

Erni

Quote
So i think the yun webserver do not found the python interpreter.


You have to edit the config file:

/etc/config/uhttpd

I have ammended this line:

list interpreter   ".cgi=/usr/bin/python"

When you are finished, restart the webserver:

/etc/init.d/uhttpd restart

I rename my python scripts with the extension .cgi,  eg  test.cgi


OttoF

thanks erni, thanks all

have insertet
list interpreter   ".py=/usr/bin/python"
in uhttpd with putty
restart and it works  :) :)

index.html
-------------------------------------------------------------
<html>
<head><title>Übertragung von Formularinhalten</title></head>
<body>

  <h1> Gib Deinen Namen ein! </h1>


  <form method="post" action="cgi_formular_auswertung.py" target="_blank">
    <p>Dein Name: <input type="text" name="Anwender"/></p>
    <p>Passwort : <input type="password" name="Passwort"/></p>
    <p><textarea name="Textfeld" cols="50" rows="10"></textarea></p>

    <p><input type="submit" name="submit"/></p> 

  </form>

</body>

</html>
--------------------------------------------------------------
cgi_formular_auswertung.py

#-*- coding: utf-8 -*-

import cgi

# Das aufrufende Programm ist ein normales html-Dokument
# In Python wird, ähnlich wie in PHP, Python-Code gemischt mit
# HTML-Anweisungen.
# Mit einer Instanz der Klasse FieldStorage können wir auf durch
# POST oder GET übertragene Daten zugreifen.
# Lange Texte können mit print """ mehrzeiliger Text """ ausgegeben
# werden. Die Angabe der Sprachcodierung UTF-8 ermöglicht es, direkt
# mit Umlauten in HTML-Texten zu arbeiten.

print "Content-type: text/html; charset=UTF-8"
print                                 # Leerzeile (muss sein!)


print """
      <html>
      <head><title>Übertragung von Formularinhalten</title></head>
         <body>
     <h1>Die übertragenen Daten.</h1>
      """

form=cgi.FieldStorage()
s1=form["Anwender"].value
s2=form["Passwort"].value

if form.getvalue("Textfeld"):
   text= form.getvalue("Textfeld")
else:
   text= "keine Eingabe"

print " <p>Dein Name lautet, :", s1,"</p>"
print "<p>Passwort : ",s2,"</p>"

print "<p>Der folgende Text wurde eingegeben:",text,"</p>"
print " </body> </html>"
--------------------------------------------------------------------------


federicofissore



gregoriusus

OttoF, can you please post HelloWorld example of .py file.

I inserted: list interpreter   ".py=/usr/bin/python", restart uhttpd, but when accessing .py file, I get error

"Bad Gateway The process did not produce any response"

I think that I have to properly format py file or response in it. Since I am new to python, please provide me with print "Hello world" example of python file.

PCWorxLA


i dont understand this because my statik webside on yun works well

arduino.local/sd/myside/index.html

just when i call

arduino.local/sd/myside/my_dynamic_side.py

i got the answer from yun to download the file.
This means the file is allready placed on yun but the webserver have no idea what is to do with the file.

So i think the yun webserver do not found the python interpreter.


Of course not, as a Python source file is not per se a web browser file like the html file is.
You basically are trying to create a catch 22 situation:
You need to first invoke the web browser with an html file, which can trigger the Python script being called, which in turn emitts more html code, which the web browser then can execute/interpret...

[ur=http://docs.python.org/2/howto/webservers.htmll]HOWTO Use Python in the web[/url]

Ralf

gregoriusus

Ok! Found out.

This is minimal Hello world example of python web served script:

print
print "Hello World!"

juanp-delacruz

Hi!

I tried setting the "uhttpd" file and have not succeeded. Could you explain how to access "/ etc / config / uhttpd"? and how can I make the setting "list interpreter" .cgi = / usr / bin / python? 

yo llevo un mes intentando y no lo he conseguido  :(

sonnyyu


Go Up