lunes, 22 de enero de 2018

python web

#!/usr/bin/env python
print "Content-Type: text/html\n"
var1=1
var2=2
str="Ambiorix_Rodriguez"
total=var1+var2
print("<h1> The sum of 1 + 2 is equal to : %s</h1>" % total)
print(str.count("o")) ## how many times 1 string is present

NOTE chmod 755 myscript.py

https://docs.python.org/2/howto/webservers.html



We’ll be using a freshly installed server with Ubuntu Server 16.04, Ubuntu Desktop should also work.
By default Ubuntu 16.04 ships with Python 3 but you won’t be able to call it if you issue the following at shell:
You’ll instead be greeted by this error message:
Instead if you issue the following, you’ll get the proper version:
So to get the OS to call python when we just type python at the terminal we need to do the following:
Note: If you have an older version of python installed you will need to first remove the link to it and then create the new one, here’s how:
Moving on, time to install the package manager for python, pip
Now to install Apache, 
Create a directory to hold the python files
Next, to register python with apache we must disable multithreading processes
Now to give apache permission to run the scripts
Next, modify apache’s configuration to set Python files as runnable and allow them to be executed
Add the following block right under the Virtualhost start declaration, usually right after the first line:
Next, change Apache’s default document root to your newly created pythontest directory, look for the word DocumentRoot in the same config and change to:
Restart Apache:
Now it’s time to test things, create a index.py file in the set directory and add the sample Hello World script:
Add the following:
Save, and hit your server’s IP in your browser and you should see the “Hello World!” text.
Note: Python3 requires updated syntax to function properly, you may see HTTP 500 Internal Server errors with the old syntax
DEBUG OPTION 
tail -f /var/log/apache2/error.log

Web configuration
nano /etc/apache2/sites-enabled/000-default.conf


<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

No hay comentarios:

Publicar un comentario