openstack 模块eventlet example code
2013-02-06 15:01
141 查看
终于有时间可以仔细研究下openstack依赖的第三方module,主要还是以官方的example入手,熟悉example的思路。
example 01 : 01webcrawler.py
比较简单,使用greenpool和urllib2 直接去抓URL 返回的内容
example 02 :02wsgiserver.py
实现简单的wsgi server
example 03 :03echoserver.py
实现简单的wsgi server
example 04 : 04socketconnection.py
socket 网络编程的客户端实现
example 05 : userChat
051ChatServer.py
052ChatClient.py
参考信息:
http://eventlet.net/doc/basic_usage.html#greenthread-spawn
example 01 : 01webcrawler.py
比较简单,使用greenpool和urllib2 直接去抓URL 返回的内容
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 1. webcrawler """ import eventlet from eventlet.green import urllib2 urls = [ "https://www.google.com/intl/en_ALL/images/logo.gif", "http://python.org/images/python-logo.gif", "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif", ] def fetch(url): print "starting webcrawler",url body = urllib2.urlopen(url).read() print "done with", url return url,body def main(): greenpool = eventlet.GreenPool(200) for url,body in greenpool.imap(fetch,urls): print "got body from ",url,"of length",len(body) if __name__ == "__main__": main() """ Result: ---------------------------------------------------------------------- starting webcrawler https://www.google.com/intl/en_ALL/images/logo.gif starting webcrawler http://python.org/images/python-logo.gif starting webcrawler http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif done with http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif done with http://python.org/images/python-logo.gif done with https://www.google.com/intl/en_ALL/images/logo.gif got body from https://www.google.com/intl/en_ALL/images/logo.gif of length 8558 got body from http://python.org/images/python-logo.gif of length 2549 got body from http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif of length 1874 """
example 02 :02wsgiserver.py
实现简单的wsgi server
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 2. wsgi servers """ import eventlet from eventlet import wsgi def hellohandler(env, start_response): if env['PATH_INFO'] != '/' : start_response('404 not found',[('Content-Type','test/plain')]) return ['Not Found\r\n'] start_response('200 ok',[('Content-Type','text/plain')]) return ['hello world!\r\n'] def main(): wsgi.server(eventlet.listen(('0.0.0.0',9257)),hellohandler) if __name__ == '__main__' : main() """ 01Testing: curl http://192.168.32.XX:9257/v1 01Result: 192.168.32.15 - - [06/Feb/2013 10:43:32] "GET /v1 HTTP/1.1" 404 120 0.000624 02Testing: curl http://192.168.32.XX:9257/ 02Result: 192.168.32.15 - - [06/Feb/2013 10:43:48] "GET / HTTP/1.1" 200 116 0.000514 """
example 03 :03echoserver.py
实现简单的wsgi server
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 2. wsgi servers """ import eventlet from eventlet import wsgi def hellohandler(env, start_response): if env['PATH_INFO'] != '/' : start_response('404 not found',[('Content-Type','test/plain')]) return ['Not Found\r\n'] start_response('200 ok',[('Content-Type','text/plain')]) return ['hello world!\r\n'] def main(): wsgi.server(eventlet.listen(('0.0.0.0',9257)),hellohandler) if __name__ == '__main__' : main() """ 01Testing: curl http://192.168.32.XX:9257/v1 01Result: 192.168.32.15 - - [06/Feb/2013 10:43:32] "GET /v1 HTTP/1.1" 404 120 0.000624 02Testing: curl http://192.168.32.XX:9257/ 02Result: 192.168.32.15 - - [06/Feb/2013 10:43:48] "GET / HTTP/1.1" 200 116 0.000514 """
example 04 : 04socketconnection.py
socket 网络编程的客户端实现
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 2. socket connection and greenpile Note: the pile acts as a collection of return values from the functions """ import eventlet from eventlet.green import socket def handler(url): ''' 01 create socet 02 get the ip by dns 03 connect 04 socket send 05 return socket recv ''' sock = socket.socket() ip = socket.gethostbyname(url) sock.connect((ip, 80)) print "%s connect" % url sock.sendall('GET /\r\n\r\n') return sock.recv(1024) def main(): ''' 01 use greenpile collect the recv from urls 02 handler the socet.sendall and recv ''' urls = ['www.bing.com', 'www.yandex.ru', 'www.python.org'] pile = eventlet.GreenPile() for url in urls: pile.spawn(handler,url) for url, result in zip(urls, pile): print "%s : %s" % (url, repr(result)[:50]) if __name__ == '__main__' : main() ''' Result: www.yandex.ru connect www.python.org connect www.bing.com connect www.bing.com : 'HTTP/1.0 400 Bad Request\r\nServer: AkamaiGHost\r www.yandex.ru : '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tran www.python.org : '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN" '''
example 05 : userChat
051ChatServer.py
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 2. socket connection and greenpile Note: the pile acts as a collection of return values from the functions """ import eventlet from eventlet.green import socket def handler(url): ''' 01 create socet 02 get the ip by dns 03 connect 04 socket send 05 return socket recv ''' sock = socket.socket() ip = socket.gethostbyname(url) sock.connect((ip, 80)) print "%s connect" % url sock.sendall('GET /\r\n\r\n') return sock.recv(1024) def main(): ''' 01 use greenpile collect the recv from urls 02 handler the socet.sendall and recv ''' urls = ['www.bing.com', 'www.yandex.ru', 'www.python.org'] pile = eventlet.GreenPile() for url in urls: pile.spawn(handler,url) for url, result in zip(urls, pile): print "%s : %s" % (url, repr(result)[:50]) if __name__ == '__main__' : main() ''' Result: www.yandex.ru connect www.python.org connect www.bing.com connect www.bing.com : 'HTTP/1.0 400 Bad Request\r\nServer: AkamaiGHost\r www.yandex.ru : '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tran www.python.org : '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN" '''
052ChatClient.py
#! /usr/bin/env python # vim: tabstop=4 shiftwidth=4 softtabstop=4 """ Eventlet examle coding 2. socket connection and greenpile Note: the pile acts as a collection of return values from the functions """ import eventlet from eventlet.green import socket def handler(url): ''' 01 create socet 02 get the ip by dns 03 connect 04 socket send 05 return socket recv ''' sock = socket.socket() ip = socket.gethostbyname(url) sock.connect((ip, 80)) print "%s connect" % url sock.sendall('GET /\r\n\r\n') return sock.recv(1024) def main(): ''' 01 use greenpile collect the recv from urls 02 handler the socet.sendall and recv ''' urls = ['www.bing.com', 'www.yandex.ru', 'www.python.org'] pile = eventlet.GreenPile() for url in urls: pile.spawn(handler,url) for url, result in zip(urls, pile): print "%s : %s" % (url, repr(result)[:50]) if __name__ == '__main__' : main() ''' Result: www.yandex.ru connect www.python.org connect www.bing.com connect www.bing.com : 'HTTP/1.0 400 Bad Request\r\nServer: AkamaiGHost\r www.yandex.ru : '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Tran www.python.org : '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN" '''
参考信息:
http://eventlet.net/doc/basic_usage.html#greenthread-spawn
相关文章推荐
- openstack 模块eventlet example code
- Openstack源代码分析之Eventlet
- openstack任务eventlet和python的GIL
- openstack基础之eventlet
- eventlet python模块学习记录
- OpenStack Eventlet
- python eventlet 模块学习tpool和greenpool
- Openstack Eventlet分析(1)
- Openstack Eventlet分析(1)
- OpenStack Eventlet分析(二)
- Openstack Eventlet分析(2)
- 菜鸟关于 aps.net<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 的认识
- pylab_examples example code: finance_demo.py
- 读Zepto源码之Event模块
- Nodejs实战心得之eventproxy模块控制并发
- HowTO: Create an Event Log Source in code, without the Permission errors
- Driver development code example.
- 探索 OpenStack 之(17):计量模块 Ceilometer 中的数据收集机制(转)
- The Beauty of Eventlet
- 【Python】threading.Event模块控制多线程