Показаны сообщения с ярлыком python. Показать все сообщения
Показаны сообщения с ярлыком python. Показать все сообщения

суббота, 15 января 2011 г.

How to install Django to Ubuntu 10.10 with installed LAMP

$sudo apt-get install python-django
$sudo apt-get install libapache2-mod-wsgi

now check django in python:
$python
import django

Must be

Python 2.5.2 (r252:60911, Jan  3 2011, 21:39:33)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django

else run
$export PYTHONPATH=$PYTHONPATH:/usr/lib/pymodules/python2.6/

mkdir ~/www/
vi django.wsgi

#!/usr/bin/env python

import os, sys

path = '/home/USER/www'
if path not in sys.path:
    sys.path.append(path)


os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' #point to settings.py file
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

django-admin startproject myproject
cd myproject
django-admin runapp myapp


cd /etc/apache2/conf.d/
sudo vi wsgi.conf

Listen 7080

      WSGIScriptAlias / /home/gennad/www/mysite/django.wsgi


ESC+:x

Now go to http://localhost:7080

вторник, 28 декабря 2010 г.

How I installed Python 2.5.2 to Ubuntu:

1. Install source code from python.org
2. Unpack to /home/<username>/python2.5.2
3. ./configure --prefix=/home/<username>/python2.5.2
4. make && make install
5. ln -s python /usr/local/bin/python2.5

вторник, 30 ноября 2010 г.

Как разобрать XML в Python? / How to parse XML with Python?

from xml.dom import minidom                                    
xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')
print xmldoc.toxml()
childnodes = xmldoc.childNodes
for childnode in childnodes:
  name = one_child.nodeName
  if name == "id":
    id = childnode.nodeValue

Как создать статический метод в классе Python? / How to create static method in Python class?

class SomeClass:
  def some_method(somekey):
    #...
  some_method = Callable(some_method)

#now works
SomeClass.some_method(some_key)

Как использовать memcache в AppEngine с Python? / How to use memcache in AppEngine with Python?

class AppEngineCache:
  lifetime = 3600 #in seconds, 1 hour


  def get_data(key):
    data = memcache.get(key)
    if data is not None:
      return data
    else:
      data = self.query_for_data()
      memcache.add(key, data, 60) #key, value, time in seconds
      #or set multiple values:
      memcache.set_multi({ "USA_98105": "raining","USA_94105": "foggy","USA_94043":    
          "sunny"
          },key_prefix="weather_", time=3600)
    return data

Как отправить POST-запрос в Python? / How to send POST request with Python?


#with httplib
import httplib
def printText(txt):
    lines = txt.split('\n')
    for line in lines:
        print line.strip()

httpServ = httplib.HTTPConnection("127.0.0.1", 80)
httpServ.connect()

quote = "test"
httpServ.request('POST', '/cgi_form.cgi', 'name=Brad&quote=%s' % quote)

response = httpServ.getresponse()
if response.status == httplib.OK:
    print "Output from CGI request"
    printText (response.read())

httpServ.close()

Как отправить GET-запрос в Python? / How to send GET-request with Python?


#with httplib
import httplib  


def printText(txt):     
  lines = txt.split('\n')     
  for line in lines:         
    print line.strip()  


httpServ = httplib.HTTPConnection("127.0.0.1", 80) 
httpServ.connect()  
httpServ.request('GET', "/test.html")  
response = httpServ.getresponse() 
if response.status == httplib.OK:     
  print "Output from HTML request"     
  printText (response.read())  
  
httpServ.request('GET', '/cgi_form.cgi?name=Brad&quote=Testing.')  
response = httpServ.getresponse() 
if response.status == httplib.OK:     
        print "Output from CGI request"     
        printText (response.read())  
httpServ.close()

Как прочитать контент веб-страницы на Python? / How to read web content with Python?


#with urllib2
import urllib2
response = urllib2.urlopen(url)
content = response.read()
print content

вторник, 23 ноября 2010 г.