Category Archives: Python

How to Generate a Random Number in Python

Check out the code snippet below to see how it works to generate a number between 1 and 100.

import random
for x in range(10):
  print random.randint(1,101)

The code above will print 10 random values of numbers between 1 and 100. The second line, for x in range(10), determines how many values will be printed (when you use range(x), the number that you use in place of x will be the amount of values that you’ll have printed. if you want 20 values, use range(20). use range(5) if you only want 5 values returned, etc.). Then the third line: print random.randint(1,101) will automatically select a random integer between 1 and 100 for you. The process is fairly simple.

 

Source 

Apache / Python basic setup

Apache / Python basic setup

This is a from the ground up basic Apache/ Python integration.

Linux OS: Debian

1. apt-get install python apache2

2. enable the cgi module by running ” a2enmod cgi ”

3. update the Apache config setting. For me I set AllowOverride to All because i wanted to set my configs via a .htaccess file.

<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

cat .htaccess
Options +ExecCGI
AddHandler cgi-script .py

3a. You can also avoid using the .htaccess file and update the apache setting to
<Directory /var/www/cgi-bin>
Options ExecCGI
SetHandler cgi-script
</Directory>

<Directory /var/www/html/>
Options +ExecCGI
AddHandler cgi-script .py
</Directory>

reference
http://httpd.apache.org/docs/2.2/howto/cgi.html

Check Apache loaded modules
apachectl -t -D DUMP_MODULES
httpd -t -D DUMP_MODULES

Note: I heard using the cgi module is discourage and they recommend using fastcgi or uwsgi. I will write up another blog entry after I run a few test. As of this writing I needed to get something up quick for a project launch.