tornado zbar 二维码识别 ,配合nginx 反向代理,supervisord 监控
1、zbar识别二维码程序,python2.6.6
#!/usr/bin/env python # coding: u8 import os import zbar import Image import urllib import uuid import requests import sys import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import requests import sys import re from tornado.options import define, options define("port", default=9000, help="run on the given port", type=int) def qrRead(url): proxies = {"http": "http://weibh:[email protected]:2012/",} uuid1 = uuid.uuid1() filename=str(uuid1)+".jpg" #print uuid1 #urllib.urlretrieve(url, filename) r=requests.get(url,proxies=proxies,stream=True) with open(filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) f.flush() f.close() # create a reader scanner = zbar.ImageScanner() # configure the reader scanner.parse_config('enable') # obtain image data pil = Image.open(filename).convert('L') width, height = pil.size #pil.show() raw = pil.tostring() # wrap image data image = zbar.Image(width, height, 'Y800', raw) # scan the image for barcodes scanner.scan(image) tmpdata='' # extract results for symbol in image: # do something useful with results print symbol.type, '图片内容为:\n%s' % symbol.data tmpdata=tmpdata+symbol.data # clean up del(image) os.remove(filename) return tmpdata class MainHandler(tornado.web.RequestHandler): def get(self): #self.write('hello'+sys.argv[1]) self.set_header("Content-Type", "text/plain") #url = 'http://www.prepolino.ch/sprache/trennen/bilder/test.gif' #qrRead(url) url=self.get_argument("url") self.write(qrRead(url)) def post(self): self.set_header("Content-Type", "text/plain") #url = 'http://www.prepolino.ch/sprache/trennen/bilder/test.gif' #qrRead(url) url=self.get_argument("url") self.write(qrRead(url)) def main(port): tornado.options.parse_command_line() application = tornado.web.Application([ (r"/", MainHandler), ]) http_server = tornado.httpserver.HTTPServer(application) http_server.listen(port) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__': port=int(sys.argv[1]) main(port)
2、下面给出我的supervisord的配置。请各位看官根据自己的实际情况修改对应的root用户。
;/etc/supervisord.conf [unix_http_server] file = /var/run/supervisor.sock chmod = 0777 chown= root:dabao [inet_http_server] # Web管理界面设定 port=127.0.0.1:9001 username = dabao password = dabao [supervisorctl] ; 必须和'unix_http_server'里面的设定匹配 serverurl = unix:///var/run/supervisord.sock [supervisord] logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (log level;default info; others: debug,warn,trace) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=true ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) user=root ; (default is current user, required if root) childlogdir=/var/log/ ; ('AUTO' child log dir, default $TEMP) [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [program:groupworker] command=python3 /home/dabao/8887.py 88%(process_num)02d process_name=%(program_name)s_%(process_num)02d numprocs=3 numprocs_start=1 autostart = true startsecs = 5 redirect_stderr = true stdout_logfile_maxbytes = 20MB stdoiut_logfile_backups = 20 stdout_logfile = /var/log/groupworker.log
3、最后第三个配置文件是nginx的反向代理配置。
nginx 1.4.4 /etc/nginx/conf.d/tornado.conf upstream tornado { server 127.0.0.1:8887; server 127.0.0.1:8888; server 127.0.0.1:8889; } server { listen 8090; location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Scheme $scheme; proxy_pass http://tornado; } }