Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Saturday, April 4, 2015

用 Python 绘制音乐图谱

用 Python 绘制音乐图谱
Original post is from http://www.christianpeccei.com/
And Articles by: PyPer

Very nice. Might worth the time to try to reproduce.

Tuesday, March 10, 2015

Django framework

To tell if you have Django installed with Python:
$ python -c "import django; print(django.get_version())"

Setup Django on bluehost:

[1] Python 2.7 + Django 1.4 on Bluehost
[2] Django on Bluehost in Five Minutes
[3] Django documentation
[4] Django documentation - tutorial

To access database using Django, see:
[5] Writing your first Django app, part 1 - create project and model (database)
[6] Writing your first Django app, part 2 - create admin site superuser
      basically it's by command: $ python manage.py createsuperuser

== Appendix 1. Python 2.7 + Django 1.4 on Bluehost ==

To avoid loss of previous web page, the basic process [1] is copied here:
Monday, April 30, 2012
Python 2.7 + Django 1.4 on Bluehost

Bluehost is a cheap shared hosting provider, that allows to run applications using fastCGI, among others, webapps created with django - python web framework. In this post you will find information how to install the newest (in April 2012) versions of python and django and how to configure them for  bluehost.

First you need to enable ssh access to your bluehost account. Sign in to their control panel, enter Security > SSH/Shell Access and click Manage SSH Access button. Select SSH Access enabled and submit. Now connect via ssh to your bluehost account.

Python
Download, extract and install python 2.7 in your home directory:
mkdir python27
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
tar xzvf Python-2.7.2.tgz
cd Python-2.7.2
./configure -prefix=/homeX/your_username/python27 --enable-unicode=ucs4
make
make install

Rename the python binary (to avoid overriding current system python version - if you're not using default bluehost python for anything else you can skip this step):
mv ~/python27/bin/python ~/python27/bin/python27
Add new python directory with binaries to your PATH environment variable. To do this edit .bashrc file in your HOME directory, for example with vim:
vim ~/.bashrc
and add the line at the end of the .bashrc file:
PATH=/homeX/your_username/python27/bin:$PATH
load the new settings:
source .bashrc
and then test it:
python27
You should see something similar to:
Python 2.7.2 (default, Apr 11 2012, 01:29:09)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

New python is available for you. Remember to use command python27 instead of python from now on.

Pip
We're going to use pip here - easy to use python package manager. It requires setuptools before it can be installed:

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar xzvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python27 setup.py install
cd
wget http://pypi.python.org/packages/source/p/pip/pip-1.1.tar.gz
tar xzvf pip-1.1.tar.gz
cd pip-1.1
python27 setup.py install

Now thanks to our earlier PATH settings pip is available from command line.

Django installation
With pip you can install latest stable Django (in my case 1.4) simply by entering:
pip install Django
Wait for download and installation process to complete. After that django is installed and command django-admin.py available. You will also probably need python driver for mysql database. Install it with pip:
pip install MySQL-python
If you're going to use PosgreSQL install psycopg2 library:
pip install psycopg2

Django configuration
Create django project:
django-admin.py startproject myproject

Decide what url should be used to access your django app. In my case it will be subdirectory:
http://somedomain.com/myproject

Prepare directory in bluehost public_html:
mkdir public_html/myproject
cd public_html/myproject


In this directory create fastcgi file (for example: mysite.fcgi) with content:
#!/homeX/your_username/python27/bin/python27
import sys, os

# Add a custom Python path.
sys.path.insert(0, "/homeX/your_username/python27")
sys.path.insert(13, "/homeX/your_username/myproject")

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")

Remember to change /homeX/your_username to your home directory path on bluehost. Change myproject.settings to correct project name. The first line contains path to your custom python binary.

Django fastcgi requires flup so install it with pip:
pip install flup
Also change file permissions to mysite.fcgi:
chmod 0755 mysite.fcgi

Now create .htaccess file in the public_html/myproject directory with content:
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ mysite.fcgi/$1 [QSA,L]


That's all. You should see your django start page on: http://somedomain.com/myproject. You can start coding some real stuff in django now. Remember to install every python and django packages using pip - that way they will be available to your django app. Also keep in mind that every django commands like syncdb, collectstatic  etc. should be run using python27 for example:
python27 manage.py syncdb


== Appendix 2. Python/Django setup on Mac ==

-- Intall wget on Mac --

Note, on Mac, to install wget, you need to download from http://ftp.gnu.org/gnu/wget/, and then do:
./configure --with-ssl=openssl
make
make install

See:
[5] http://coolestguidesontheplanet.com/install-and-configure-wget-on-os-x/

-- Install setuptools --

For more recent version of setup tools, follow instruction at:
[6] https://pypi.python.org/pypi/setuptools

You may need superuser privilege:
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python

-- Install mysql-python --

When install mysql-python, it may report "EnvironmentError: mysql_config not found". See
[7] http://stackoverflow.com/questions/25459386/mac-os-x-environmenterror-mysql-config-not-found
In that case, do:

> locate mysql_config
# this should report something like: /usr/local/mysql-5.5.18-osx10.6-x86_64/bin/mysql_config.
# then add it to current path:
> PATH=$PATH:/usr/local/mysql-5.5.18-osx10.6-x86_64/bin/
> mysql_config
# this should show that mysql_config is available. Finally:
> sudo pip install mysql-python

-- Install mod_fcgid --

On mac, you may need to install mod_fcgid. Without mod_fcgid, will get this error:
/mysite.fcgi/ was not found on this server.
E.g.: http://stackoverflow.com/questions/24446496/deploying-django-error-mysite-fcgi-was-not-found-on-this-server-in-shared

To install mod_fcgid, you need to install macport first. See:
[8] http://www.dionysopoulos.me/apache-mysql-php-server-on-mac-os-x-with-multiple-simultaneous-php-versions/

To install macport, go to: http://www.macports.org/install.php. Use "pkg" installer.
Then do:
sudo port selfupdate
sudo port install mod_fcgid
sudo cp /opt/local/apache2/modules/mod_fcgid.so /usr/libexec/apache2

Then: sudo vi /etc/apache2/httpd.conf

Find all the LoadModule lines. After the last one add:
LoadModule fcgid_module libexec/apache2/mod_fcgid.so

Then restart apache:
sudo apachectl restart


== Appendix 3. Use Python in the web ==

Use Python in the web: https://docs.python.org/2/howto/webservers.html

Save code below as a.fcgi. This can run as fastcgi.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from cgi import escape
import sys, os
from flup.server.fcgi import WSGIServer

def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])

    yield 'Use Python in the web: https://docs.python.org/2/howto/webservers.html'
    yield '<h1>FastCGI Environment</h1>'
    yield '<table>'
    for k, v in sorted(environ.items()):
         yield '<tr><th>%s</th><td>%s</td></tr>' % (escape(k), escape(v))
    yield '</table>'

WSGIServer(app).run()


== Appendix 4. Install rest_framework ==

sudo pip install djangorestframework


Sunday, June 29, 2014

A Python daemon

A simple unix/linux daemon in Python. This is for Python 2.x. The article also contains a link for Python 3.x (link is here).

The python-daemon library is another solution.

Thursday, February 6, 2014

Python code to log in a site

Below is Python code to log in a site and submit a page by post:

"""
# Script to log in to website and store cookies.
# run as: python web_login.py USERNAME PASSWORD
#
# from: http://martinjc.com/2011/06/09/logging-in-to-websites-with-python/
#
# sources of code include:
#
# http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies
# http://stackoverflow.com/questions/301924/python-urllib-urllib2-httplib-confusion
# http://www.voidspace.org.uk/python/articles/cookielib.shtml
#
# mashed together by Martin Chorley
# modified by HomeTom
#
# Licensed under a Creative Commons Attribution ShareAlike 3.0 Unported License.
# http://creativecommons.org/licenses/by-sa/3.0/
"""

import urllib, urllib2
import cookielib
import sys

class WebLogin(object):

    def __init__(self, username, password):
      
        # url for website we want to log in to
        self.base_url = 'http://baseurl.com'
        # login action we want to post data to
        # could be /login or /account/login or something similar
        self.login_action = '/account/login.php'
        # file for storing cookies
        self.cookie_file = 'login.cookies'

        # user provided username and password
        self.username = username
        self.password = password

        # set up a cookie jar to store cookies
        self.cj = cookielib.MozillaCookieJar(self.cookie_file)

        # set up opener to handle cookies, redirects etc
        self.opener = urllib2.build_opener(
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(self.cj)
        )

        # pretend we're a web browser and not a python script
        self.opener.addheaders = [('User-agent',
            ('Mozilla/4.0 (compatible; MSIE 6.0; '
            'Windows NT 5.2; .NET CLR 1.1.4322)'))
        ]

        # open the front page of the website to set and save initial cookies
        response = self.opener.open(self.base_url)
        self.cj.save()

        # try and log in to the site
        response = self.login()
        #print response.read()

        data = urllib.urlencode({
            'fieldName1' : 'fieldValue1',
            'fieldName2' : 'fieldValue2',
            'btnSubmit'  : "submit"
        })

        response = self.opener.open("http://baseurl.com/func.php", data)
        print response.read()

      
    # method to do login
    def login(self):

        # parameters for login action
        # may be different for different websites
        # check html source of website for specifics
        login_data = urllib.urlencode({
            'username' : self.username,
            'password' : self.password,
            'btnLogin' : "submit"
        })

        # construct the url
        login_url = self.base_url + self.login_action
        # then open it
        response = self.opener.open(login_url, login_data)
        # save the cookies and return the response
        self.cj.save()
        return response


if __name__ == "__main__":

    args = sys.argv

    # check for username and password
    if len(args) != 3:
        print "Incorrect number of arguments"
        print "Argument pattern: username password"
        exit(1)

    username = args[1]
    password = args[2]

    # initialise and login to the website
    WebLogin(username, password)

Blog Archive

Followers