Python bottle support for JSONP

最近用bottle写后台api接口,返回的都是json格式。后来写前端的同学说不能解析json,然后搜了下终于知道有JSONP这个东东,然后知道jsonp的格式原来就是一个callback函数名称xxx(请求中带有callback=xxx)包着一个json数据的方法的形式。其实JSONP只是用来跨域通信用的,虽然现在的需求不需要用jsonp,不过某同学没有研究出来js里面怎么直接获得同一域JSON数据,所以我还是折腾了一下。挺好,又学到些东西:P

直接上代码:如果使用的是default_app,则直接把下面代码贴进对应的py文件中就ok了

from bottle import response, request, install
def jsonp(callback):
    def wrapper(*args, **kwargs):
        resp = callback(*args, **kwargs)
        if isinstance(resp, (dict,list)):
            #response.charset='utf-8' # set property error , do not know why
            response.content_type = 'application/json;utf-8' # after added this line, we do not need to mess with character encoding in json.dumps; the commented out code is what i did before
            callback_arg = request.query.get('callback')
            if callback_arg:
                resp= '{}({})'.format(callback_arg, json.dumps(resp))#,ensure_ascii=False))#.encode('utf-8')
        return resp
    return wrapper

install(jsonp) # install the plugin in the bottle app

另外就是发现,处理jsonp为这样的字符串以后,返回的结果数据的中文直接显示为unicode的字符串形式(\uxxxx形式)。解决方法:

  • 最开始我是让json模块dumps处理的字符串进行解码,然后encode成utf-8,这样输出就正常了(代码见被注释掉的倒数第五行)
  • 后来发现只需要对返回的内容格式content-type限定为json和utf-8,就自动解析正常了(代码见倒数第八行)
Advertisements

Python Bottle+virtualenv+uWSGI+Nginx installation on Ubuntu 12.04.1 LTS

bottle+uwsgi+nginx

为了造福世界人民,我觉得还是用英语写吧 XD

Today I got a request to write a backend server to provide interface access from frontend. I determined to use Python and  RESTful api firstly . I hate writing Java code…

After some investigation, I picked up Bottle. It is very neat and small. And I also read that with nginx and uWSGI, the performance is good too.

My environment is Ubuntu 12.04.1 LTS; but after followed several posts, my environment still did not work. I want to share my findings to save others’ time:)

Let’s cut to the chase:

  • Note:

My application would be in below directory constructure, all following commands and configurations are based on below:
/var/www/myapp would be the root directory of my bottle app
/var/www/myapp/env would be the virtualenv for the app
/var/www/myapp/index.py would be main .py file of bottle

  • Install nginx and uwsgi:
sudo apt-get update
sudo apt-get install -y nginx uwsgi
sudo apt-get install uwsgi-plugin-python
  • Install pip to help install python modules
sudo apt-get install python-pip
  • Install virtualenv: virtualenv to seperate the Python environment for different deployments
sudo pip install virtualenv
  • Set up app virtualenv:
sudo mkdir -p /var/www/myapp
sudo virtualenv /var/www/myapp/env
source /var/www/myapp/env/bin/activate
pip install bottle
deactivate
  • Change the permission of the app directory so that uWSGI can read it and Python can write in it
sudo chown -R www-data:www-data /var/www/myapp
  • OK, the most important part: the configuration of nginx and uWSGI. These two recipes would worth some money XD

1. nginx config:

sudo gedit /etc/nginx/sites-enabled/default

copy below config in opend file:

server {

     listen   80;
     charset utf-8;
     root /var/www/myapp;
     server_name localhost;

location / {
     include uwsgi_params;
     uwsgi_pass unix:/tmp/uwsgi.myapp.socket;
     uwsgi_param UWSGI_PYHOME /var/www/myapp/env;
     uwsgi_param UWSGI_CHIDIR /var/www/myapp;
     uwsgi_param UWSGI_SCRIPT index; # this should be the .py file name without suffix that your bottle will use to launch
     }
}

2.uWSGI config:

sudo gedit /etc/uwsgi/apps-enabled/uwsgi.ini

copy below config in opend file:

[uwsgi]
plugins=python
socket=/tmp/uwsgi.myapp.socket
pythonpath=/var/www/myapp
  • Now we are one step to success. In order to verify the setup, we need a sample index.py file ( the file name must be consistent with the one in UWSGI_SCRIPT in nginx config).

Put index.py under /var/www/myapp

#!/usr/bin/env python
from bottle import route, run, default_app
@route('/')
def index():
    return "Aloha, world~"

if __name__ == "__main__":
    run(host="localhost", port=8081)
else:
    application = default_app()
  • Finally, Restart/start nginx and uWSGI services:
sudo service nginx restart
sudo service uwsgi restart

Now “it’s the moment to witness the miracle!” 😀  Access http://localhost/ in your browser, you should  see “Aloha, world~”.  If not, comment below:)

“你画我猜”(draw something)单词猜测工具 – 升级版

前几天写的“你画我猜”(draw something)单词猜测工具,最后提到了有道已经做了相关工具。既然有大树了,俺们就可以直接乘凉了~

使用有道网页版的改进版本来了:直接发送查询请求然后对返回结果格式化就搞定了,非常easy:)(需要用到lxml模块)

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Sina Weibo @SeganW
"""
from __future__ import unicode_literals
import urllib
import lxml.html as lh
import sys

def query(char_str,num=None):
    if not None:
        num=len(char_str)
    con=None
    resp=None
    try:
        con=urllib.urlopen('http://dict.youdao.com/drawsth?letters={}&length={}'.format(char_str,num))
        resp=con.read()
    except Exception as e:
        if con:
            con.close()
        raise SystemExit('[error] guess request failed: {}'.format(e))
    doc=lh.document_fromstring(resp)
    words=[i.text_content().lower() for i in doc.xpath("//span[@class='word']")]
    translates=[i.text_content() for i in doc.xpath("//div[@class='trans']")]
    return zip(words,translates)

if __name__ == '__main__':
    args=sys.argv
    if len(args)==2:
        charstr= args[1]
        num=None
    elif len(args)==3:
        charstr= args[1]
        num=int(args[2])
    else:
        raise SystemExit('usage: drawsomthing.py charstr [num]')
    results=query(charstr,num)
    if results:
        for sub_result in results:
            print '{}:\t{}'.format(*sub_result)
    else:
        print "No results found:("

使用方法不变:

运行的命令行就是

drawsomething_updated.py char_str [num]

char_str为程序显示的乱序字符串,把让你拆的所有字符串起来做一个字符串
num是程序让你猜的单词包含的字符个数;可不填,这样就变成python版词典了:)

选择最快的appstore ip地址之Python版

今天才用上正统的Appstore软件下载方法:从iTunes端下载,然后PC端与iOS设备同步。然后俺就发现,下载速度那个慢啊。。。
从网上得到如下消息:

苹果为 App Store 准备了 a1.phobos.apple.com 到 a2000.phobos.apple.com 这么一群服务器,美国、欧洲、日韩、港澳……就是没有中国。。。

不管真假,让咱们用Python挨个ping出哪个地址最快,然后加到hosts里面吧:)
以下代码为Windows中文版,如果其它版,请把speed_ptn行的字符串改成对应的:

Nov/4/2014更新:
multiprocessing.dummy处理I/O负荷的并发,multiprocessing处理CPU负荷的并发。多少个url都无所谓啦~
注意:只写了Mac OS X版本的

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Sina weibo @SeganW
Only works in Mac OS X, I tested in Yosemite
"""
from __future__ import unicode_literals
from subprocess import check_output, CalledProcessError
from re import compile as re_compile
from sys import getfilesystemencoding
from os import devnull
from multiprocessing.dummy import Pool 

URLS = ['a{}.phobos.apple.com'.format(i) for i in range(1,2001)]
IP_PTN = re_compile('\d+\.\d+\.\d+\.\d+')
AVG_PTN = re_compile('min/avg/max/stddev = \d+\.\d+/(\d+\.\d+)/')

def ping_test(url):
    try:
        with open(devnull, 'w') as null:
            output = check_output(['ping','-c','3', url], stderr=null).decode(getfilesystemencoding())
            ip = IP_PTN.search(output).group()
            avg = float(AVG_PTN.search(output).group(1))
    except CalledProcessError:
        ip = ""
        avg = ""
    print 'Server {}\t||\t Average Speed: {}ms'.format(url,avg)
    return {ip: avg}

if __name__ == '__main__':
    pool = Pool(25) #25 is process count, try increase or decrease it to find the best performance
    results = pool.map(ping_test, URLS)
    pool.close()
    pool.join()
    best = min(results,key=lambda x: x.values()[0])
    best_svr = results.index(best)
    print '***** Best server is a{}.phobos.apple.com, ip={}, avg speed={}ms *****'.format(best_svr, best.keys()[0],best.values()[0])

Aug/20/2012更新:写了使用多线程并加入了对timeout情况的处理,发现虽然速度快了,但是结果却是每次都是最大那个数的服务器ping值最快。不知道为啥。另外并发数不能太大,URLS里面直接用2000会提示打开文件太多。改成500没发现问题。

新版代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Sina weibo @SeganW
"""
from __future__ import unicode_literals
import subprocess
import re
import sys
import threading

URLS=map(lambda x: 'a{}.phobos.apple.com'.format(x), range(1,501))
NUM=0
BEST=[2**16,'',''] # a big number normally larger than average
IP_PTN=re.compile('\[(.*)\]')
SPEED_PTN=re.compile('平均 = (\d+)ms')

class Ping(threading.Thread):
    def __init__(self, url, lock):
        threading.Thread.__init__(self)
        self.url=url
        self.lock=lock

    def run(self):
        try:
            output=subprocess.check_output(['ping',self.url]).decode(sys.getfilesystemencoding())
        except subprocess.CalledProcessError as cpe:
            print cpe.output
            output=cpe.output.decode(sys.getfilesystemencoding())
            avg='timed out'
        else:
            avg="{}ms".format((SPEED_PTN.search(output).group(1)))
        ip= IP_PTN.search(output).group(1)
        self.lock.acquire()
        print 'Server {}\t||\t Average Speed: {}'.format(self.url,avg)
        if avg =='timed out':
            pass
        elif BEST[0] > int(avg[:-2]):
            BEST[0] = int(avg[:-2])
            BEST[1] = ip
            BEST[2] = url
        global NUM
        NUM+=1
        if NUM==len(URLS):
            print 'Fastest server: {2} ( {1} ) fastest average speed: {0}ms'.format(*BEST)
        self.lock.release()

if __name__ == '__main__':
    lock=threading.Lock()
    try:
        for url in URLS:
            ping_thread=Ping(url,lock)
            ping_thread.start()
    except KeyboardInterrupt:
        pass

原版:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Sina weibo @SeganW
"""
from __future__ import unicode_literals
import subprocess
import re
import sys

urls=map(lambda x: 'a{}.phobos.apple.com'.format(x), range(1,2001))
best=[2**16,'',''] # a big number normally larger than average
ip_ptn=re.compile(r'\[(.*)\]')
speed_ptn=re.compile(r'平均 = (\d+)ms')
try:
    for url in urls:
        output=subprocess.check_output('ping {}'.format(url),shell=True).decode(sys.getfilesystemencoding())
        output=[i for i in output.split('\r\n') if i]
        ip= ip_ptn.search(output[0]).group(1)
        avg=int(speed_ptn.search(output[-1]).group(1))
        print 'Server {}\t||\t Average Speed: {}ms'.format(url,avg)
        if best[0] > avg:
            best[0] = avg
            best[1] = ip
            best[2] = url
except KeyboardInterrupt:
    print '\n'
print 'Fastest ip address:\n{1} {2}\nfastest speed: {0}ms'.format(*best)

找到最快的以后就可以改hosts文件了,这个不懂的自己google吧。

OpenCV 2.3.1 Python Bindings installation on Windows 7

Today I searched several post about how to install OpenCV Python bindings on Windows 7. And I found many of them made it too complicated.

Here is my simple solution. Many thanks to Christoph Gohlke, Laboratory for Fluorescence Dynamics, University of California, Irvine, his work makes many Python modules be easy to be installed on Windows.

Simple steps:

  1. Install numpy for python, as OpenCV 2.3.1 needs this. normally choose the MKL version and the relevant python version and OS architecture( x86 or amd64)
  2. Install OpenCV , choose the relevant python version and OS architecture( x86 or amd64) as numpy
  3. The tricky part: as opencv 2.3.1 has been evolved to cv2, the old cv functions has been moved to cv2.cv. The workaround is create a cv.py file with
    from cv2.cv import *
    
  4. Copy this cv.py file under directory %PYTHON_HOME%\Lib\site-packages
  5. Now you can import cv to wrote OpenCV code. Have fun:)

“你画我猜”(draw something)单词猜测工具

Apr.6 更新:修正一处elif写成if的bug;增加ctrl-c强制停止的支持;增加对不填字符个数的用处的说明

最近“Draw Something”很火,我也玩了几次。碰到过靠谱的搭档,也碰到过很不靠谱的搭档。当然我自己也属于不靠谱一类,哈哈。

碰到的不靠谱的人多了,猜个词就费劲了。试词语让人很不爽。

所以就想写个Python脚本干这事。

首先想到了我这边对备选的字符进行排列,然后找个网络词典的接口来进行查验。

网上能搜到的在线词典的接口比较少,有dict.cn, iciba.cn和dict.qq.com的。其实我本人想用iciba的来着,不过貌似现在不能用了。最后选择了qq的词典。json返回值,我很喜欢。

刚才找了下google translate的接口,确实有。不过1M字符=20刀的价格真是伤不起。

接口搞定,就差对所有字符排列组合了。最后俺选用了itertools.permutations方法。这个方法比较傻,对所有字符进行穷举排列;如果哪位同学有更好的算法,欢迎不吝赐教:)

好了,不多说了,上代码

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: weibo @SeganW
"""
from __future__ import unicode_literals
import urllib
import json
import itertools
import sys

def query(word):
    con=None
    try:
        #由于google translate接口收费,而且非常的贵;
        #词霸的接口现在好像用不了了,不知道谁知道新地址
        #网上能找到能使用的就剩qq词典;词典结果不是很理想;
        #比如有些单词不在本地词典中,而网络释义的结果有非常的不可信,只能退而求其次,把所有网络释义都标记上了,大家自行鉴别
        con=urllib.urlopen('http://dict.qq.com/dict?q={}'.format(word))
        resp=con.read()
    except Exception as e:
        print '[error] get {!r} definition failed: {}'.format(word,e)
        if con:
            con.close()
        return None
    resp_json=json.loads(resp)
    if resp_json.get('err'):
        return None
    elif resp_json.get('lang') != 'eng':
        return None
    else:
        descs=[]
        if resp_json.get('local'):
            try:
                loc_desc=resp_json['local'][0]['des']
            except KeyError:
                return None
            for des in loc_desc:
                try:
                    p_type=des['p']
                except KeyError as ke:
                    return None
                if p_type == 'n.':
                    descs.append(des['d'])
        elif resp_json.get('netdes'):
            for net_desc in resp_json.get('netdes'):
                net_word=net_desc.get('word')
                if net_word and net_word.lower()==word.lower():
                    try:
                        descs.append('网络释义:{}'.format(net_desc['des'][0]['d']))
                    except KeyError:
                        continue
                else:
                    return None
        else:
            return None
        if descs:
            result= '{} : {}'.format(word,';'.join(descs)).encode(sys.getfilesystemencoding())
            print result
        else:
            result=None
	return result

def guess(char_str,num=None):
    if not num:
        num=len(char_str)
    assert isinstance(char_str,str), 'not a string'
    #非常傻的穷举方法,有好的算法的同学请不吝赐教~
    pmt=itertools.permutations(char_str,r=num)
    result=[]
    for i in pmt:
        word=''.join(i)
        #print 'Guessing word {!r}'.format(word)
        guess_result=query(word)
        if guess_result:
            result.append(guess_result)
    return result

if __name__ == '__main__':
    args=sys.argv
    if len(args)==2:
        charstr= args[1]
        num=None
    elif len(args)==3:
        charstr= args[1]
        num=int(args[2])
    else:
        raise SystemExit('usage: drawsomthing.py charstr [num]')
    print """友情提醒:由于QQ词典网络释义和部分解释不准确,请自行辨别\n开始查询..."""
    try:
        guess(charstr,num)
    except KeyboardInterrupt:
        print '已强制退出'
    else:
        print "查询结束"

运行的命令行就是

drawsomething.py char_str num

char_str为程序显示的乱序字符串,把让你拆的所有字符串起来做一个字符串
num是程序让你猜的单词包含的字符个数;可不填,这样就变成python版词典了:)

刚才写这篇文章的时候发现有道词典已经做好了更先进的工具(毕竟人家有完善的词库,俺这只是个小python脚本~),大家如果真的要用的话,就去用它的吧,地址在这里