Wednesday 6 March 2013

Configure Tomcat 7 to run Python CGI scripts in windows

Pre-installation requirements
1. Java
2. Python

steps
1. Download latest version of Tomcat (Tomcat 7) from
    http://tomcat.apache.org/download-70.cgi
2. After successful installation of tomcat modify the web.xml file in the  <TOMCAT_HOME>\conf\folder ( eg: C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf)
 uncomment the cgi servlet and its mapping

<servlet>
 <servlet-name>cgi</servlet-name>
 <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
 <init-param>
 <param-name>debug</param-name>
 <param-value>0</param-value>
 </init-param>
 <init-param>
 <param-name>cgiPathPrefix</param-name>
 <param-value>WEB-INF/cgi</param-value>
 </init-param>
 <load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>cgi</servlet-name>
 <url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>



3. Add an servlet parameter "passShellEnvironment" and set it to “true” (
 "force" the environment variables to be passed)

<init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>

</init-param> 
4.  Add an servlet parameter "executable"
<init-param>
          <param-name>executable</param-name>
          <param-value>C:\Python27\python.exe</param-value>
 </init-param> 


so the overall cgi servlet in web.xml is given below
<servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
        </init-param>
        <init-param>
          <param-name>executable</param-name>
          <param-value>C:\Python27\python.exe</param-value>
        </init-param>
        <init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>
        </init-param>
         <load-on-startup>5</load-on-startup>
    </servlet> 


5.  Modify <TOMCAT_HOME>\conf\context.xml  to add a property on <Context>:
 <Context privileged="true">
...
</Context>

6. Create a folder say "test" in <TOMCAT_HOME>\webapps directory . It is the root folder for your application
7. create a WEB-INF folder inside the root folder (test) and create a cgi folder inside WEB-INF folder
8. Create a python CGI script and put in in <TOMCAT_HOME>\webapps\test\WEB-INF\cgi\

hello.py

print "Content-type: text/html\n\n";
print "Hello, world!\n"


9. start the tomcat server and browse the url
http://localhost:8080/test/cgi-bin/hello.py
 

 

Wednesday 13 February 2013

Python 2.7: Proxy Authentication error in Urllib2

Issue:

When we try to use Urllib2 in Python 2.7 in an institute which requires proxy authentication, we are getting the below error

Traceback (most recent call last):
  File "C:\Users\cYanide\Documents\Python Challenge\1\1.py", line 7, in <module>
    response = urllib2.urlopen('http://python.org/')
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
**urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required**


Solution:

We need to set the environmental variable 
HTTP_PROXY and HTTPS_PROXY.your program will pick up proper authentication method and execute the code.

To set the environmental variable please follow the below steps in windows
start-> right click My Computer -> click properties -> Click Advanced System Setting 
A pop up will open

Click Environment variables.
Environment variables pop up will open. Under User variables in that popup, click New button.
In the  new pop up add New User 
variable name : HTTP_PROXY
variable value: http://username:password@proxyserver:port
repeat the same steps for  HTTPs_PROXY also


Wednesday 6 February 2013

http_proxy password contains @ issue

When you try to set up an "http_proxy" with password which contains @, the set command won't work.

for example, if your proxy password is pass@word and you are trying to set up http_proxy using
set http_proxy="http://username:pass@word@server:port", then proxy won't set
 

To resolve this issue, you have to  percent-encode the special characters ie replace @ with %40
 set http_proxy="http://username:pass%40word@server:port"

Friday 7 September 2012

Import data into MySQL database using mysqlimport

The below example shows how to import data into mysql database using mysqlimport utility. We are importing data from a text file to mysql database.

mysqlimport utility is a stand-alone command line utility program which comes with MySQL.

First you have to connect to mysql server.
1. open command prompt
2. change directory to the path of mysql bin folder (for example C:\xampp\mysql\bin)











Create a database employee and use that database.







create a table employee with 2 fields id and name







create a text file for employee table with fields delimited by tab.This file is used for importing data into employee table.

employee.txt

1    Tom
2    pinky
3    Ram

put the employee.txt file into C:\xampp\mysql\data\employee folder.
In mysqlimport, the name of the data file should match the name of the table.  

close the mysql connection by typing exit






open a new command prompt and change directory to the path of mysql bin folder (for example C:\xampp\mysql\bin)
Type mysqlimport command 









This will import the employee details from the text file into employee table.

 

Thursday 26 July 2012

MySQL said: Documentation #2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)

After successfully installed  xampp and type http://localhost/phpmyadmin/ it opens up a page with erroe message:

MySQL said: Documentation #2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)


Fix for the issue:
1. open the file config.inc.php in the folder C:\xampp\phpMyAdmin
2. add the following code in the file
   $cfg['Servers'][$i]['socket'] = '/var/run/mysql/mysql.sock';
   (don't forget to save a local copy of config.inc.php before editting)
3. Clear the history of the browser and refresh it.

Thursday 24 May 2012

Install Python

python installation instructions

1. Download python from
2. Choose the installer for your machine (For window 7 (64 bit) click Python 2.7.3 Windows X86-64    Installer.  for windows 32 bit click Python 2.7.3 Windows Installer).
3. It starts downloading and  save it in a local folder.
4. Double the downloaded exe file ( python-2.7.3.amd64.exe.)
5. Follow the installation instructions.
6. Choose a directory for installing python.(eg: C:\python2.7.3 )
7. Click the finish button. Now Python is installed in your machine.

Running Python

To check python is properly installed, open a command window(Start -> Programs -> Accessories -> Command Prompt.) Enter the command "python" and hitting return.
If the python command, instead of displaying the interpreter prompt >>>, gives you a message like:










Then you need to make sure that your computer knows where to find the Python interpreter. To do this you will have to modify a setting called PATH, which is a list of directories where Windows will look for programs.

To set PATH variable
start-> right click My Computer -> click properties -> Click Advanced System Setting
A pop up will open .





















Click Environment variables.
Environment variables pop up will open. Under System variables in that popup select "Path" . click Edit button.






















Add the python path (eg:C:\python2.7.3\) at the end of Variable Value.
Click ok .













Now you have successfully add Python’s installation directory to to the PATH variable.


Close the command prompt and again open it and type python.
now you can see python python below message, indicates python is successfully installed. 










For running python you can use command prompt or Python IDLE. It is easy to use Python IDLE than command prompt. IDLE integrated development environment (IDE) is for  editing and running Python 2.x or Python 3 programs.

How to open python IDLE (Python GUI)

1. Start->All programs->Python 2.7 click IDLE (Python GUI). 
You will see windows entitled "Python Shell" .










2. From the Python Shell window, select New Window from the File menu.
3. A window entitled "Untitled" will open.
4. From the File menu, select Save As, and  save your Python file to a folder
5. In the File name: text box, type: hello.py (The type of file should be Python)
6.In the hello.py file type print "hello" and save the file.








7.To run hello.py, select Run Module from the Run menu
8. Then you can see the output of hello.py in a Python Shell window.














Wednesday 2 May 2012

Install standalone blast for windows

BLAST allows you to compare your sequence against a database of sequences and informs you if your sequence matches any of the sequences in the database.

The ncbi-blast-#.#.#+-win32.exe is compatible with PCs running Windows XP, 32-bit Vista or Windows 7.
ncbi-blast-#.#.#+-win64.exe is compatible with PCs running  Windows XP 2003, 64-bit Vista or Windows7. "#.#.#" denotes the current version number of the package.

Installation steps

1. Download blast((ncbi-blast-2.2.26+-win32.exe) from
    ftp://ftp.ncbi.nlm.nih.gov/blast/executables/LATEST/

2. Double click the ncbi-blast-2.2.26+-win32.exe . Accept the license agreement.Then the installer will     prompt for an installation directory. The default installation directory is "C:\blast-2.2.25+".
(you can choose any other destination folder)

3. click the install button. It will install blast in C:\blast-2.2.25+

4. Next step is to configure the environment variable.
   Go to Start -> My Computer
   Right click My Computer and click Properties.
   System Properties pop up will open.
   In the popup, click on the Advanced tab
   Click the "Environment Variables" button
   Click the "New" button under the "User variable for ..." panel.
   add
       variable name : Path
       variable value: C:\blast-2.2.25+\bin
click ok button.

5. Create the Blast database

The blast+ installer automatically creates a BLASTDB environment variable, which points to the "C:\blast-2.2.25+\" directory. For more effective database management, a "db" subdirectory should be created under the "C:\blast-2.2.25+\".
The value of BLASTDB environment variable must be modified to "C:\blast-2.2.3+\db\".
Pre-formatted NCBI BLAST databases is available as compressed archives from NCBI ftp site.You can download the database and put in the db folder. The second option is you can create a custom database. Databases can be prepared from custom FASTA sequences locally using the makeblastdb utility.

To create a custom BLAST database, you need a simple FASTA file say nuclic.fasta. Put all your FASTA sequence in it. Put the nuclic.fasta file in C:\blast-2.2.25+\db folder. The custom sequence's fasta file (nuclic.fasta)is then converted into BLAST database by a tool called makeblastdb.(you can see makeblastdb.exe file in C:\blast-2.2.25+\bin folder)

To create blast database file
Open the command promt.
The below image show the execution of makeblastdb command.














the command is
makeblastdb -in db/nuclic.fasta -dbtype nucl -out db/nuclic

It will create 3 files nuclic.nhr, nuclic.nin, nuclic.nsq in  C:\blast-2.2.25+\db folder
.nsq, .nhr, and .nin files are blastn database files.

6. Create  test.txt file which contains the FASTA sequence to be compared.

This file is used as an input query in a test blastn search with the following command line:

blastn -query db/test.txt -db db/nuclic -out db/output.txt

which performs the following tasks:
    1. calling blastn to run a nucleotide query vs nucleotide database search
    2. using sequence contained in test.txt file as input query
    3. searching against nuclic database
    4. saving the result in output.txt
The below image show the execution of blastn command.















A new file output.txt is created in C:\blast-2.2.25+\db folder.