Python script in the browser

This site shows you how to setup apache2 so you can run python script in the browser (lampp). He shows a method on how to use an html form to fetch date from python using POST.

Running a script directly without POST and using urllib2 to fetch a website:
in the browser type:
http://localhost/cgi-bin/web.py

script:
#!/usr/bin/python
import cgi
import cgitb
import urllib2
cgitb.enable()

# Begin HTML generation, must be here
print "Content-Type: text/html; charset=UTF-8" # Print headers
print "" # Signal end of headers

# HTML is pushed to the page with print
# To save time you can use ''' to open and close a print
# statement for block printing

response = urllib2.urlopen('http://facebook.com')
print response.read()

print "hello world"

tags:
run python script html apache2 server
run python script html
urllib2 execute html

Multiple Sites on One Server – Virtual Hosts

This works for Ubuntu

Go to your site directory and make two folder site1.com and site2.com
$ mkdir /var/www/site1.com /var/www/site2.com

Modify your host file
$ sudo nano /etc/hosts

Add the two lines of code into that file
127.0.0.1 site1.com
127.0.0.1 site2.com

Make configuration changes to apache by creating 2 files in the /etc/apache2/sites-available directory
$ sudo touch /etc/apache2/sites-available/site1.com /etc/apache2/sites-available/site2.com

Put this code in those new created file.
site1.com file:
<VirtualHost *:80>
DocumentRoot /var/www/site1.com
ServerName www.site1.com
ServerAlias site1.com
</VirtualHost>

site2.com file:
<VirtualHost *:80>
DocumentRoot /var/www/site2.com
ServerName site2.com
ServerAlias site2.com
</VirtualHost>

Final configuration commands
$ sudo a2ensite site1.com
$ sudo a2ensite site2.com
$ sudo service apache2 reload

Now if you put either site1.com or site2.com in your browser you’ll go to those sites. Now if you own these domain names put you internet IP address with the domain hosting and it will automatically navigate to the specified site location, making accessibility public.


if error:
apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName

$ sudo nano /etc/apache2/httpd.conf

add the line below:
ServerName localhost

Resource.

tags:
AH00558: apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message

Corrupted dpkg status file fix

Today I was trying to install a program using apt-get install however I kept getting this error

.
.
.
Do you want to continue [Y/n]? Y
Selecting previously unselected package libxmlrpc-core-c3.
(Reading database … 60%dpkg: unrecoverable fatal error, aborting:
reading files list for package ‘intltool-debian’: Input/output error
E: Sub-process /usr/bin/dpkg returned an error code (2)

after hours of research I found out that the reason for this error was the /var/lib/dpkg/status file was corrupted.

I had three options to fix this which were reinstall ubuntu operating system, edit the status files or find a backup status file. I didn’t want to reinstall my operating system so I tried the other two.

(warning: Before trying to edit any file make sure you make a back up of it)

I tried to edit the status file by looking for the package I was trying to install. However I couldn’t find it and I didn’t know what to delete. Luckly there’s dpkg status backupfiles in your system located at /var/backups. I found one that was created a month ago called dpkg.status.3.gz and extracted it and renamed the file status and put into /var/lib/dpkg directory.

Then I tried to install the program using apt-get install and it worked.

Create executable ubuntu icons for python script

First create you python script
pyqt_test.py:

#!/usr/bin/env python3

import sys
from PyQt4 import QtGui, QtCore
 
class HelloQt(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
 
        self.setWindowTitle("Hello Qt")
 
        # Vertical layout
        self.layout = QtGui.QVBoxLayout(self)
 
        # Create widgets
        self.label = QtGui.QLabel("What's your name?")
        self.name = QtGui.QLineEdit()
        self.output = QtGui.QLineEdit()
        self.output.setReadOnly(True)
 
        # Add widgets to the layout
        self.layout.addWidget(self.label)
        self.layout.addWidget(self.name)
        self.layout.addWidget(self.output)
 
        # Connect self.name with self.sayHello(name) when text is changed
        self.connect(self.name, QtCore.SIGNAL("textChanged(const QString&)"), self.sayHello)
 
    def sayHello(self, name):
        # Set the output text
        self.output.setText("Hello " + name + "!")
 
if __name__ == "__main__":
        # Create QApplication
        app = QtGui.QApplication(sys.argv)
 
        # Create a HelloQt window and show it
        helloqt = HelloQt()
        helloqt.show()
       
        # Run the mainloop
        sys.exit(app.exec_())

make sure that your python script has the shebang on the first line.
Make pyqt_test.py an executable file by running
$ chmod +x pyqt_test.py

Now create the Icon runPythonIcon.desktop:

[Desktop Entry]
Type=Application
Name=python gui
Exec=/home/bigbang/Desktop/python/pyqt_test.py 
Icon=/usr/share/icons/xchats_icon

make this file also executable by running
$ chmod +x runPythonIcon.desktop

Done.

Resource

Math is so Useful

If you want to know how the world works? Know math. -My College history teacher

Rarely do I use math in PHP but when it comes to circuit building, business and statistics. Math is my best friend.

Ask anyone, what is their favorite subject and rarely will you ever hear math. You’ll hear stuff like “I hate math, why do I have to learn this stuff”. Yeah math is a pain to learn. My Algebra teacher Ms. Kline told me that math is different from any other subject, you’ll actually need to practice it in order to be good.

I was trying to find out which distributors offered a better price at what quantity.  They had different fixed and variable cost. It’s a 2 simple linear equations.  q is the quantity and p is the total price.

Distributer A Distributer B
Price (q) $2 $3
Shipping $5 $0
Equation 2q+5= p 3q=p

I used wolframalpha to solve the equation.

When I solved the equation I get that q=5 and p =15.  This means that when the quantity is 5 both distributor have the same price but when quantity is less than 5 distributor A has the better price and when quantity is more than 5 distributor B has the better price.