Skip to content

Raspberry Apache run py cgi scripts

How to enable .py scripts in Apache2

By default Python is configured to only execute scripts with specific endings such as .cgi. In order to make Apache execute .py files, you need to edit a configuration file. In a terminal type this command:

sudo leafpad /etc/apache2/sites-enabled/000-default &

This file contains settings for the default site (you can create other sites on the same server). Look for a section of code that looks like this:

<Directory “/usr/lib/cgi-bin”>

</Directory>

Add the following line inside the above section of code:

AddHandler cgi-script .py

In a terminal window, type this command to reload Apache’s configuration files:

sudo service apache2 reload

Now type this code into a text editor, and save it as /usr/lib/cgi-bin/hello.py:

#!/usr/bin/env python
print “Content-type: text/html\n\n”
print “<h1>Hello World</h1>”

Make the file executable using this command:

$ sudo chmod +x hello.py

As in the previous post, you can execute this script by accessing it through your browser. The IP address of my Raspberry Pi is 192.168.0.4, so I typed this address into my browser: http://192.168.0.4/cgi-bin/hello.py. Once again, you should see the words “Hello World” in large letters.

http://raspberrywebserver.com/cgiscripting/writing-cgi-scripts-in-python.html

Advertisements

Raspberry serial

Disable Serial Port Login

To enable the serial port for your own use you need to disable login on the port. There are two files that need to be edited

The first and main one is /etc/inittab

This file has the command to enable the login prompt and this needs to be disabled. Edit the file and move to the end of the file. You will see a line similar to

T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

Disable it by adding a # character to the beginning. Save the file.

#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

http://www.hobbytronics.co.uk/raspberry-pi-serial-port

Monitor Raspberry temperature online

The simplest way to be able to see the temperature of your raspberry is to write it in the /var/www/index.html.

You will need to set up the webserver as described in the previous post. Then write a simple script that reads in the temperature and then prints it in the index.html. Then run the script automatically as frequently as you wish with the help of the crontab software. You can set it to every minute, every hour, every two days or whatever.

So,

  1. nano /usr/local/bin/getTemp.sh

#!/bin/bash
cpuTemp0=$(cat /sys/class/thermal/thermal_zone0/temp)
cpuTemp1=$(($cpuTemp0/1000))
cpuTemp2=$(($cpuTemp0/100))
cpuTempM=$(($cpuTemp2 % $cpuTemp1))


echo CPU temp"="$cpuTemp1"."$cpuTempM"'C" >> /var/www/index.html
echo GPU $(/opt/vc/bin/vcgencmd measure_temp) >> /var/www/index.html
timestamp() {
date +"%T"
}
TZ=":US/Central"
echo "Time: $(date)" >> /var/www/index.html
echo "<br>"/var/www/index.html

2. Then make it executable:

chmod a+x /usr/local/bin/getTemp.sh

3. Then add it to crontab:

crontab -e
and add to the file:
*/5 * * * * sudo /usr/local/bin/getTemp.sh

to run it every 5 minutes.

Raspberry Pi webserver

This is mostly a note to myself about what I do when setting up a web server on the pi.

  • Install one
    • sudo apt-get install apache2 -y
  • This time I won’t need php, so I will install that later.
  • The wbserver started running automatically after installation, so just punch in the Pi’s IP address to the browser and it should send you to the index page of the Webserver.

After this, I want to make it available from the web too, not only my local network. It is connected to the router via an ethernet cable, but I think the router would assign a new IP to it after each restart.

So I need to give a static IP to it so it will be the same after every restart, so in the router settings I can set up port forwarding for it.

Setting the PI’s ip to 192.168.1.103 static (more info):

    • sudo nano /etc/network/interfaces
    • or sudo vi /etc/network/interfaces depending on editor preference


auto lo


iface lo inet loopback
iface eth0 inet static
address 192.168.1.103
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1


allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

Then it should take effect after restart:

  • sudo reboot

Now for this IP we need to set up port forward in the routers setting page. Get the user manual of your router and set up http portforward for the Pi’s ip (in my case 192.168.1.103) on port 80.

After that, get your outside IP, type in the browser get my IP and then type that in to the browser.

Mine is:

76.84.48.83

You should see what is running on my PI’s webserver, by clicking on it.

Python list comprehension avagy lista kód rövidebben és hatékonyabban

This is how to do it the long way:

panelTemp=[]

d=0
for i in itotal:
    panelTemp.append(float(dryBulbTemperature[d])+(i*40/1000))
    d+=1

 

The short way:

panelTemp=[float(d)+(i*40/1000) for d,i in zip(dryBulbTemperature,itotal)]

 

For average of a list:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l) / len(l)
Image

Data usage on my nexus 4 in 1 month

image

Ubuntu 12.10 Dell inspiron 17r special edition 7720 install

There are so many descriptions available, I still feel like I need to make my own because the ones I read make the process too complex.

The result after the process:

Windows 8 and Ubuntu 12.10 “dualboot”, the reason for the “” is that dual boot won’t mean the same thing as on earlier machines. Ubuntu is going to be default at boot, and win8 can be chosen by pressing F12 right after power button pushed then selecting win.

I didn’t put any ubuntu directories on the SSD because it works fast enough without doing so, and I want to use the intel SRT on win.

Preparation on Win8:

  1. Set up the partition for linux from the built in partition manager of win: win button ->type: control panel -> type: partition. I gave a 100GB to the ubuntu but choose whatever you want to, I would give it at least 15GB.
  2. Disable the SRT: control panel-> type: intel ->  choose intel smart response technology -> accelerate tab -> disable.
  3. Download and put ubuntu 12.10 64 bit on a pendrive, use the LinuxLive Usb creator http://www.linuxliveusb.com/

Boot from pendrive:

  1. Restart, F12 at boot and choose boot from USB drive.
  2. Try ubuntu in live mode
  3. run the installer and install it on the partition you prepared for it under win. If it doesn’t see the partitions, don’t panic just leave a comment.

Everything works fine under ubuntu, even the fan speed is fine.

Known issues with this laptop and ubuntu:

  1. Touchpad scrolling doesn’t work initially.