Skip to content

Commit a5d1c78

Browse files
committed
init
0 parents  commit a5d1c78

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+3994
-0
lines changed

Eventlet/dispatch_pattern.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# coding: utf8
2+
"""A simple web server that accepts POSTS containing a list of feed urls,
3+
and returns the titles of those feeds.
4+
"""
5+
import eventlet
6+
import sys
7+
feedparser = eventlet.import_patched('feedparser')
8+
default_encoding = sys.getfilesystemencoding()
9+
if default_encoding.lower() == 'ascii':
10+
default_encoding = 'utf-8'
11+
12+
# the pool provides a safety limit on our concurrency
13+
pool = eventlet.GreenPool()
14+
15+
def fetch_title(url):
16+
d = feedparser.parse(url)
17+
return d.feed.get('title', '')
18+
19+
def app(environ, start_response):
20+
if environ['REQUEST_METHOD'] != 'POST':
21+
start_response('403 Forbidden', [])
22+
return ['403 Forbidden']
23+
24+
# the pile collects the result of a concurrent operation -- in this case,
25+
# the collection of feed titles
26+
pile = eventlet.GreenPile(pool)
27+
for line in environ['wsgi.input'].readlines():
28+
url = line.strip()
29+
if url:
30+
pile.spawn(fetch_title, url)
31+
# since the pile is an iterator over the results,
32+
# you can use it in all sorts of great Pythonic ways
33+
titles = '\n'.join(pile)
34+
start_response('200 OK', [('Content-type', 'text/plain')])
35+
return [titles]
36+
37+
38+
if __name__ == '__main__':
39+
from eventlet import wsgi
40+
wsgi.server(eventlet.listen(('localhost', 9010)), app)

Eventlet/eventlet_db_profile.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
import eventlet
3+
import time
4+
import random
5+
import MySQLdb
6+
import eventlet.db_pool as db_pool
7+
from DBUtils.PooledDB import PooledDB
8+
9+
conn_kwargs={'host':'10.241.226.41', 'user':'root', 'passwd':'feisky','db':'news', 'charset': 'utf8'}
10+
sql="""select * from news limit 1"""
11+
pooled=db_pool.ConnectionPool(MySQLdb, **conn_kwargs)
12+
pooldb=PooledDB(MySQLdb, **conn_kwargs)
13+
14+
def query(conn):
15+
cur=conn.cursor()
16+
#cur.execute(sql % (random.randint(1,1000)))
17+
cur.execute(sql)
18+
data=cur.fetchall()
19+
cur.close()
20+
return data
21+
22+
def test_mysqldb(times):
23+
now = time.time()
24+
for i in range(times):
25+
conn=MySQLdb.connect(**conn_kwargs)
26+
r=query(conn)
27+
print r
28+
conn.close()
29+
print 'MySQLdb: ', time.time()-now
30+
31+
def test_eventlet(times):
32+
now = time.time()
33+
for i in range(times):
34+
conn=pooled.get()
35+
try:
36+
r=query(conn)
37+
print r
38+
finally:
39+
pooled.put(conn)
40+
print 'Eventlet: ', time.time()-now
41+
42+
def test_dbutils(times):
43+
now = time.time()
44+
for i in range(times):
45+
conn=pooldb.connection()
46+
try:
47+
r=query(conn)
48+
print r
49+
finally:
50+
conn.close()
51+
print 'DBUtils: ', time.time()-now
52+
53+
#test_mysqldb(10000) # 50s
54+
#test_eventlet(10000) # 31s
55+
test_dbutils(10000) # 30s
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
import eventlet
3+
4+
def handle(client,address):
5+
print 'Client ', address, ' connected'
6+
while True:
7+
c=client.recv(10000)
8+
if not c: break
9+
print c
10+
client.sendall(c)
11+
12+
server=eventlet.listen(('0.0.0.0', 6000))
13+
pool=eventlet.GreenPool(10000)
14+
while True:
15+
new_sock, address = server.accept()
16+
pool.spawn_n(handle, new_sock, address)

Eventlet/wsgi_server.log

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[2013-04-10 13:37:47] [INFO] __main__ Client 10.241.227.16 connected
2+
[2013-04-10 13:37:47] [INFO] __main__ Client 10.241.227.16 connected
3+
[2013-04-10 13:37:47] [INFO] __main__ Client 10.241.227.16 connected
4+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
5+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
6+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
7+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
8+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
9+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
10+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
11+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
12+
[2013-04-10 13:37:48] [INFO] __main__ Client 10.241.227.16 connected
13+
(11026) wsgi starting up on http://0.0.0.0:9010/
14+
10.241.227.16 - - [10/Apr/2013 13:37:47] "GET / HTTP/1.1" 200 1210 0.002784
15+
10.241.227.16 - - [10/Apr/2013 13:37:47] "GET /favicon.ico HTTP/1.1" 200 1124 0.000771
16+
10.241.227.16 - - [10/Apr/2013 13:37:47] "GET /favicon.ico HTTP/1.1" 200 1124 0.000754
17+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET / HTTP/1.1" 200 1210 0.000771
18+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000734
19+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000755
20+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET / HTTP/1.1" 200 1210 0.000747
21+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000740
22+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000735
23+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET / HTTP/1.1" 200 1210 0.000768
24+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000738
25+
10.241.227.16 - - [10/Apr/2013 13:37:48] "GET /favicon.ico HTTP/1.1" 200 1124 0.000737
26+
wsgi exiting
27+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
28+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
29+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
30+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
31+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
32+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
33+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
34+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
35+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
36+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
37+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
38+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
39+
[2013-04-10 13:42:15] [INFO] __main__ Client 10.241.227.16 connected
40+
[2013-04-10 13:42:16] [INFO] __main__ Client 10.241.227.16 connected
41+
[2013-04-10 13:42:16] [INFO] __main__ Client 10.241.227.16 connected
42+
[2013-04-10 13:42:16] [INFO] __main__ Client 10.241.227.16 connected
43+
[2013-04-10 13:42:16] [INFO] __main__ Client 10.241.227.16 connected
44+
[2013-04-10 13:42:16] [INFO] __main__ Client 10.241.227.16 connected
45+
[2013-04-10 13:52:00] [INFO] __main__ Client 10.241.227.16 connected
46+
[2013-04-10 13:52:00] [INFO] __main__ Client 10.241.227.16 connected
47+
[2013-04-10 13:52:00] [INFO] __main__ Client 10.241.227.16 connected
48+
[2013-04-10 13:52:23] [INFO] __main__ Client 10.241.227.16 connected
49+
[2013-04-10 13:52:23] [INFO] __main__ Client 10.241.227.16 connected
50+
[2013-04-10 13:52:23] [INFO] __main__ Client 10.241.227.16 connected

Eventlet/wsgi_server.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python
2+
"""A simple web server that accepts POSTS containing a list of feed urls,
3+
and returns the titles of those feeds.
4+
"""
5+
import eventlet
6+
import sys
7+
import logging
8+
from eventlet.green import urllib
9+
10+
default_encoding = sys.getfilesystemencoding()
11+
if default_encoding.lower() == 'ascii':
12+
default_encoding = 'utf-8'
13+
14+
# the pool provides a safety limit on our concurrency
15+
pool = eventlet.GreenPool()
16+
logging.basicConfig(format='[%(asctime)s] [%(levelname)s] %(name)s %(message)s',
17+
datefmt='%Y-%m-%d %H:%M:%S',)
18+
#filename='wsgi_server.log')
19+
logger = logging.getLogger(__name__)
20+
logger.setLevel(logging.DEBUG)
21+
22+
def get_client_address(environ):
23+
try:
24+
return environ['HTTP_X_FORWARDED_FOR'].split(',')[-1].strip()
25+
except KeyError:
26+
return environ['REMOTE_ADDR']
27+
28+
def get_request_path(environ):
29+
return environ.get('PATH_INFO', '')
30+
31+
def get_request_params(environ):
32+
params={}
33+
param=environ.get('QUERY_STRING', '')
34+
if len(param)>0:
35+
param_list=urllib.unquote(param).split('&')
36+
for para in param_list:
37+
key,value=para.split('=')
38+
params[key]=value
39+
return params
40+
41+
def get_env(env):
42+
from pprint import pformat
43+
return '%s\r\n' % pformat(env)
44+
45+
def app(environ, start_response):
46+
if environ['REQUEST_METHOD'] != 'GET':
47+
start_response('403 Forbidden', [])
48+
return ['403 Forbidden']
49+
50+
# the pile collects the result of a concurrent operation
51+
pile = eventlet.GreenPile(pool)
52+
pile.spawn(get_request_path, environ)
53+
pile.spawn(get_env, get_request_params(environ))
54+
client_ip = get_client_address(environ)
55+
logger.info('Client %s connected', client_ip)
56+
pile.spawn(get_env, environ)
57+
# since the pile is an iterator over the results,
58+
# you can use it in all sorts of great Pythonic ways
59+
message = '\n'.join(pile)
60+
start_response('200 OK', [('Content-type', 'text/plain;charset=utf-8')])
61+
return [message]
62+
63+
if __name__ == '__main__':
64+
from eventlet import wsgi
65+
wsgi.server(eventlet.listen(('0.0.0.0', 9010)), app) #, log=file('wsgi_server_.log','a+'))

Eventlet/wsgi_server_.log

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(11314) wsgi starting up on http://0.0.0.0:9010/
2+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET / HTTP/1.1" 200 1210 0.001674
3+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000763
4+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000774
5+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET / HTTP/1.1" 200 1210 0.000764
6+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000759
7+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000812
8+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET / HTTP/1.1" 200 1210 0.000762
9+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000767
10+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET / HTTP/1.1" 200 1210 0.000776
11+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000754
12+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.000743
13+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET / HTTP/1.1" 200 1210 0.000769
14+
10.241.227.16 - - [10/Apr/2013 13:42:15] "GET /favicon.ico HTTP/1.1" 200 1124 0.001065
15+
10.241.227.16 - - [10/Apr/2013 13:42:16] "GET / HTTP/1.1" 200 1210 0.000861
16+
10.241.227.16 - - [10/Apr/2013 13:42:16] "GET /favicon.ico HTTP/1.1" 200 1124 0.000786
17+
10.241.227.16 - - [10/Apr/2013 13:42:16] "GET /favicon.ico HTTP/1.1" 200 1124 0.000785
18+
10.241.227.16 - - [10/Apr/2013 13:42:16] "GET /favicon.ico HTTP/1.1" 200 1124 0.000749
19+
10.241.227.16 - - [10/Apr/2013 13:42:16] "GET /favicon.ico HTTP/1.1" 200 1124 0.000751
20+
wsgi exiting
21+
(11582) wsgi starting up on http://0.0.0.0:9010/
22+
10.241.227.16 - - [10/Apr/2013 13:52:00] "GET /?from=fetch&lang=%E4%B8%AD%E6%96%87 HTTP/1.1" 200 1265 0.001679
23+
10.241.227.16 - - [10/Apr/2013 13:52:00] "GET /favicon.ico HTTP/1.1" 200 1124 0.000760
24+
10.241.227.16 - - [10/Apr/2013 13:52:00] "GET /favicon.ico HTTP/1.1" 200 1124 0.000786
25+
10.241.227.16 - - [10/Apr/2013 13:52:23] "GET /get?from=fetch&lang=%E4%B8%AD%E6%96%87 HTTP/1.1" 200 1232 0.000908
26+
10.241.227.16 - - [10/Apr/2013 13:52:23] "GET /favicon.ico HTTP/1.1" 200 1124 0.000757
27+
10.241.227.16 - - [10/Apr/2013 13:52:23] "GET /favicon.ico HTTP/1.1" 200 1124 0.000743
28+
wsgi exiting

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
python-tutorials
2+
================
3+
4+
Some tutorials for python.

algo/ProjectEuler/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>projecteuler</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>

algo/ProjectEuler/.pydevproject

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?>
3+
4+
<pydev_project>
5+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6+
<path>/projecteuler</path>
7+
</pydev_pathproperty>
8+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
9+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
10+
</pydev_project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Sat Mar 03 21:15:32 CST 2012
2+
eclipse.preferences.version=1
3+
encoding/p10.py=UTF-8

0 commit comments

Comments
 (0)