Saturday, November 3, 2012
Tornado: non-blocking web server
Tornado is an non-blocking, scalable web server.
It can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services
Install:
Download package from http://www.tornadoweb.org/ and install from python.
With tornado you will get many demo projects for helloworld, appengine, auth, blog, chat, facebook, s3server so on.
Posted by
Abu Zahed Jony
at
2:56 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
Python non-blocking web server,
Tornado
django hello world
Django is python high level framework. It always encourage rapid development and clean, pragmatic design.
It lets you build high-performing, elegant web applications quickly. You can also develop back-end API(django have some cool library for it). Lets go....
Create a django project.
From command prompt:
Create django project: django-admin.py startproject django_hello_world
Create django app: python manage.py startapp hello
I am using pycharm, so I can create my project from pycharm.
Here I show you only which file I need to edit for run hello world project.
You can download full projcet from here
running project:
// settings.py
# set admin
ADMINS = (
('jony', 'jony.cse@gmail.com'),
)
#set database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
# Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dev.db',
# Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '',
# Set to empty string for localhost. Not used with sqlite3.
'PORT': '',
# Set to empty string for default. Not used with sqlite3.
}
}
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'hello', # need to install your app
)
//views.py
# create a view function which we will call
from django.http import HttpResponse
def hello_view(request):
return HttpResponse("Hello my django!!")
//url.py
#call view from url
from hello import views
urlpatterns = patterns('',
url(r'^hello_django/$', views.hello_view, name='my_hello_view'),
)
commands: python manage.py syncbd # when you make chage on db, need to run this command # create super user python manage.py runserver 8080 Not hit this url: http://127.0.0.1:8080/hello_django/
Posted by
Abu Zahed Jony
at
1:16 AM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
django
django install
Install from pip:
pip install Django
or
download django package and install by command python manage.py install
Posted by
Abu Zahed Jony
at
1:08 AM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
django
Beautiful Soup CSS selector
Beautiful Soup supports a subset of the CSS selector standard. Just construct the selector as a string and pass it into the .select() method of a Tag or the BeautifulSoup object itself.
I used this html file for practice.
All source code available on github
from pprint import pprint
from bs4 import BeautifulSoup
html_content = open('bs_sample3.html')
# http://dl.dropbox.com/u/49962071/blog/python/resource/bs_sample3.html
soup = BeautifulSoup(html_content) # making soap
pprint(soup.select("title")) # get title tag
pprint(soup.select("body a")) # all a tag inside body
pprint(soup.select("html head title")) # html->head->title
pprint(soup.select("head > title")) # head->title
pprint(soup.select("p > a")) # all a tag that inside p
pprint(soup.select("body > a")) # all a tag inside body
pprint(soup.select(".sister")) # select by class
pprint(soup.select("#link1")) # select by id
pprint(soup.select('a[href="http://example.com/elsie"]'))
# find tags by attribute value
pprint(soup.select('a[href^="http://example.com/"]'))
# find tags by attribute value, all contains 'http://example.com/'
pprint(soup.select('p[lang|=en]')) # Match language codes
Posted by
Abu Zahed Jony
at
12:11 AM
4
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
Beautiful Soup
Friday, November 2, 2012
Beautiful Soup find_all() search API
find_all() is the most popular method in the Beautiful Soup search API. It's reduce your code size massively. We can use
regular expression, custom function into it. I used this html file for practice.
All source code available on github
from pprint import pprint
import re
from bs4 import BeautifulSoup
html_content = open('bs_sample.html')
#http://dl.dropbox.com/u/49962071/blog/python/resource/bs_sample.html
soup = BeautifulSoup(html_content) # making soap
for tag in soup.find_all(re.compile("^p")): # find all tag start with p
print tag.name
for tag in soup.find_all(re.compile("t")): # find all tag contains t
print tag.name
for tag in soup.find_all(True): # find all tag
print tag.name
pprint(soup.find_all('a')) # find all a tag
print 20*"++"
pprint(soup.find_all(["a", "b"])) # find multiple tag
def has_class_but_no_id(tag):
return tag.has_key('class') and not tag.has_key('id')
pprint(soup.find_all(has_class_but_no_id))
# pass a function to find_all
pprint(soup.find_all(text=re.compile("sisters")))
# find all tag content contains key 'sisters'
print 20*"++"
pprint(soup.find_all(href=re.compile("my_url"))) # all links contains key "my_url"
pprint(soup.find_all(id=True)) # all links has id
pprint(soup.find_all(class_=True)) # all links has class
def has_six_characters(css_class):
return css_class is not None and len(css_class) == 7
pprint(soup.find_all(class_=has_six_characters))
# find all class name contains 7 characters
pprint(soup.find_all("a", "sister")) # find all a tag have class named 'sister'
pprint(soup.find_all("a", re.compile("sister")))
# find all a tag have class named contains 'sister'
print 20*"++"
pprint(soup.find_all(href=re.compile("elsie"), id='link1'))
# url name contains elsie and have id = link1
pprint(soup.find_all(attrs={'href' : re.compile("elsie"), 'id': 'link1'}))
# url name contains elsie and have id = link1
pprint(soup.find_all("a", limit=2)) # use limit on find_all
pprint(soup.html.find_all("title", recursive=True)) # use recursive on findall
Posted by
Abu Zahed Jony
at
11:30 PM
1 comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
Beautiful Soup
explore git
Posted by
Abu Zahed Jony
at
7:56 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
git
Beautiful Soup 4 exploring
Quick explore to Beautiful Soup 4. I used this
document for practicing. All source codes of this blog available on github.
from pprint import pprint
from bs4 import BeautifulSoup
html_content = open('bs_sample.html')
# http://dl.dropbox.com/u/49962071/blog/python/resource/bs_sample.html
soup = BeautifulSoup(html_content) # making soap
print soup.prettify() # prettify html_content even complete uncompleted tag
print soup.title # page title tag
print soup.title.name # page title name
print soup.title.parent.name # page title parent
print soup.p # first p tag
print soup.p.string # string content of first p tag
print soup.p['class'] # first p tag class name
print soup.a # first a tag
pprint( soup.find_all('a')) # all a tag
pprint( soup.find_all('p')) # all p tag
print soup.find(id='link3') # find tag with id = link3
print 'All links:'
for link in soup.find_all('a'):
print link.get('href') # get url
print soup.get_text() # return text part of html_document
Posted by
Abu Zahed Jony
at
7:26 PM
0
comments
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels:
Beautiful Soup
Subscribe to:
Posts (Atom)
