openstack 模块eventlet example code

终于有时间可以仔细研究下openstack依赖的第三方module,主要还是以官方的example入手,熟悉example的思路。

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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值