Python Lover(6)Twisted and Basic
1. Installation and Version Upgrade on Python
Download the package from official website. I get these 2 version, 2.7.10 and 3.4.3 for mac.
https://www.python.org/
Verify the Installation
> python -V
Python 2.7.6
> python2 -V
Python 2.7.10
> python3 -V
Python 3.4.3
tools pip http://zhonghuan.info/2014/10/01/pip%E4%BB%8B%E7%BB%8D%E4%B8%8E%E4%BD%BF%E7%94%A8/
Set up the right version, I plan to use 2.7.10, because a lot of tools and other things, they are not supporting python3 right now.
> rm -fr /usr/local/bin/python
> rm -fr /usr/bin/python
> sudo ln -s /usr/local/bin/python2 /usr/bin/python
> python -V
Python 2.7.10
Download pip file
> wget https://bootstrap.pypa.io/get-pip.py
Install pip
> python get-pip.py
Verify the version
> pip -V
pip 7.1.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (python 2.7)
Install or upgrade setuptools
> pip install -U setuptools
Upgrade pip if needed
> pip install -U pip
Use pip to install latest Django
> pip install Django==1.8.4
Verify the Django installation
> python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.8.4
How we use Django there
http://sillycat.iteye.com/blog/2116834
http://sillycat.iteye.com/blog/2116836
Download and use pycharm for Python IDE
https://www.jetbrains.com/pycharm/
Download Twisted
> wget http://twistedmatrix.com/Releases/Twisted/15.4/Twisted-15.4.0.tar.bz2
Unzip the file and go to the working directory
> cd Twisted-15.4.0
Install from the source
> python setup.py install
Check the version after installation.
>>> print twisted.version
[twisted, version 15.4.0]
2. Understand Model
single-threaded synchronous task1 —> task2 —> task3
the threaded model task1 —>
task2 —>
task3 —>
Asynchronous model task1 —> task2 —>task1 —>task3 —>task2 —>task1 ...
The last model works best>
a. There are a large number of tasks so there is likely always at least one task that can make progress.
b. The tasks perform lots of I/O, causing a synchronous program to waste lots of time blocking
c. The tasks are largely independent
working on these links
http://twistedmatrix.com/documents/current/web/howto/
http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html
http://lingxiankong.github.io/blog/2013/12/23/python-setup/
http://developer.51cto.com/art/201003/189317.htm
http://yansu.org/2013/05/15/learn-how-to-use-distutils.html
http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/
browser ——> request/response ——>HTTP render, Template system.
3. Objects and Twisted
Site Objects
from twisted.web import server, resource
from twisted.internet import reactor
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
return "<html>Hello, Sillycat.</html>"
site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()
Visit this URL will work http://localhost:8080/
Twistd web Server
> twistd web --help
Usage: twistd [options] web [web options]
Here is the command to start twistd server
> twistd web --path ./ --port 8080 --logfile ./twistd.log
Then, we can visit http://localhost:8080/ to see the current directory.
This command will kill the process
> kill `cat twistd.pid`
http://twistedmatrix.com/documents/current/index.html
http://twistedmatrix.com/documents/current/web/howto/web-in-60/
Dynamic Web Content
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
import time
class ClockPage(Resource):
isLeaf = True
def render_GET(self, request):
return "<html><body>%s</body></html>" % (time.ctime(),)
resource = ClockPage()
factory = Site(resource)
reactor.listenTCP(8880, factory)
reactor.run()
URL Dispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/static-dispatch.html
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web.static import File
root = Resource()
root.putChild("foo", File("/tmp"))
root.putChild("bar", File("/lost+found"))
root.putChild("baz", File("/opt"))
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
File —> Resource —>Site —> reactor
http://codereview.stackexchange.com/questions/45060/simple-rest-api-server
http://jacek99.github.io/corepost/doc/build/html/index.html
Dynamic URL Dispatch
http://twistedmatrix.com/documents/current/web/howto/web-in-60/dynamic-dispatch.html
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from calendar import calendar
class YearPage(Resource):
def __init__(self, year):
Resource.__init__(self)
self.year = year
def render_GET(self, request):
return "<html><body><pre>%s</pre></body></html>" % (calendar(self.year),)
class Calendar(Resource):
def getChild(self, name, request):
return YearPage(int(name))
root = Calendar()
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
Site - the object which associates a listening server port with the HTTP implementation
Resource - a convenient base class to use when defining custom pages
reactor - the object which implements the Twisted main loop
References:
Twisted
http://krondo.com/?p=1209
https://twistedmatrix.com/trac/
https://github.com/twisted/twisted
Twisted Intro
https://github.com/jdavisp3/twisted-intro
http://krondo.com/?page_id=1327
Chinese Version
https://github.com/luocheng/twisted-intro-cn
http://turtlerbender007.appspot.com/twisted/index.html
Python Basic
http://sillycat.iteye.com/blog/562783 Basic Syntax
http://sillycat.iteye.com/blog/568997 Operator and Function
http://sillycat.iteye.com/blog/569004 If, else, while, and, or, not
http://sillycat.iteye.com/blog/569827 for, array [], string, split, join
http://sillycat.iteye.com/blog/569837 List (), random, dict {},
http://sillycat.iteye.com/blog/570849 try: except: , raise OutOfRangeError(‘out of range’)
http://sillycat.iteye.com/blog/570862 class, copy, def function in class
http://sillycat.iteye.com/blog/570882 __init__, __str__, __add__, __sub__, private function __methodName
example
https://github.com/orangain/pika-twisted-example
https://www.digitalocean.com/community/tutorials/how-to-package-and-distribute-python-applications
distutils
http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-19/
http://yansu.org/2013/05/15/learn-how-to-use-distutils.html
http://developer.51cto.com/art/201003/189317.htm
http://lingxiankong.github.io/blog/2013/12/23/python-setup/
web
http://twistedmatrix.com/documents/current/web/howto/
http://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html