Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
42a56843
Commit
42a56843
authored
May 30, 2011
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: use py3k compatible syntax
parent
d212ebb3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
22 deletions
+29
-22
examples/concurrent_download.py
examples/concurrent_download.py
+2
-2
examples/dns_mass_resolve.py
examples/dns_mass_resolve.py
+6
-4
examples/echoserver.py
examples/echoserver.py
+4
-4
examples/geventsendfile.py
examples/geventsendfile.py
+3
-1
examples/processes.py
examples/processes.py
+9
-7
examples/webproxy.py
examples/webproxy.py
+5
-4
No files found.
examples/concurrent_download.py
View file @
42a56843
...
...
@@ -15,9 +15,9 @@ import urllib2
def
print_head
(
url
):
print
'Starting %s'
%
url
print
(
'Starting %s'
%
url
)
data
=
urllib2
.
urlopen
(
url
).
read
()
print
'%s: %s bytes: %r'
%
(
url
,
len
(
data
),
data
[:
50
]
)
print
(
'%s: %s bytes: %r'
%
(
url
,
len
(
data
),
data
[:
50
])
)
jobs
=
[
gevent
.
spawn
(
print_head
,
url
)
for
url
in
urls
]
...
...
examples/dns_mass_resolve.py
View file @
42a56843
...
...
@@ -7,6 +7,7 @@ This script splits the job between a number of greenlets to get the
results faster.
"""
from
__future__
import
with_statement
import
sys
import
gevent
from
gevent
import
socket
from
gevent.pool
import
Pool
...
...
@@ -22,9 +23,10 @@ def job(url):
try
:
try
:
ip
=
socket
.
gethostbyname
(
url
)
print
'%s = %s'
%
(
url
,
ip
)
except
socket
.
gaierror
,
ex
:
print
'%s failed with %s'
%
(
url
,
ex
)
print
(
'%s = %s'
%
(
url
,
ip
))
except
socket
.
gaierror
:
ex
=
sys
.
exc_info
()[
1
]
print
(
'%s failed with %s'
%
(
url
,
ex
))
finally
:
finished
+=
1
...
...
@@ -33,4 +35,4 @@ with gevent.Timeout(2, False):
pool
.
spawn
(
job
,
'%s.com'
%
x
)
pool
.
join
()
print
'finished within 2 seconds: %s/%s'
%
(
finished
,
N
)
print
(
'finished within 2 seconds: %s/%s'
%
(
finished
,
N
)
)
examples/echoserver.py
View file @
42a56843
...
...
@@ -11,7 +11,7 @@ from gevent.server import StreamServer
# this handler will be run for each incoming connection in a dedicated greenlet
def
echo
(
socket
,
address
):
print
'New connection from %s:%s'
%
address
print
(
'New connection from %s:%s'
%
address
)
# using a makefile because we want to use readline()
fileobj
=
socket
.
makefile
()
fileobj
.
write
(
'Welcome to the echo server! Type quit to exit.
\
r
\
n
'
)
...
...
@@ -19,10 +19,10 @@ def echo(socket, address):
while
True
:
line
=
fileobj
.
readline
()
if
not
line
:
print
"client disconnected"
print
(
"client disconnected"
)
break
if
line
.
strip
().
lower
()
==
'quit'
:
print
"client quit"
print
(
"client quit"
)
break
fileobj
.
write
(
line
)
fileobj
.
flush
()
...
...
@@ -34,5 +34,5 @@ if __name__ == '__main__':
server
=
StreamServer
((
'0.0.0.0'
,
6000
),
echo
)
# to start the server asynchronously, use its start() method;
# we use blocking serve_forever() here because we have no other jobs
print
'Starting echo server on port 6000'
print
(
'Starting echo server on port 6000'
)
server
.
serve_forever
()
examples/geventsendfile.py
View file @
42a56843
...
...
@@ -2,6 +2,7 @@
[1] http://pypi.python.org/pypi/py-sendfile/
"""
from
sys
import
exc_info
from
errno
import
EAGAIN
from
sendfile
import
sendfile
as
original_sendfile
from
gevent.socket
import
wait_write
...
...
@@ -14,7 +15,8 @@ def gevent_sendfile(out_fd, in_fd, offset, count):
_offset
,
sent
=
original_sendfile
(
out_fd
,
in_fd
,
offset
+
total_sent
,
count
-
total_sent
)
#print '%s: sent %s [%d%%]' % (out_fd, sent, 100*total_sent/count)
total_sent
+=
sent
except
OSError
,
ex
:
except
OSError
:
ex
=
exc_info
()[
1
]
if
ex
[
0
]
==
EAGAIN
:
wait_write
(
out_fd
)
else
:
...
...
examples/processes.py
View file @
42a56843
...
...
@@ -28,8 +28,9 @@ def popen_communicate(args, data=''):
try
:
# p.stdin.write() doesn't return anything, so use os.write.
bytes_written
+=
os
.
write
(
p
.
stdin
.
fileno
(),
data
[
bytes_written
:])
except
IOError
,
ex
:
if
ex
[
0
]
!=
errno
.
EAGAIN
:
except
IOError
:
ex
=
sys
.
exc_info
()[
1
]
if
ex
.
args
[
0
]
!=
errno
.
EAGAIN
:
raise
sys
.
exc_clear
()
socket
.
wait_write
(
p
.
stdin
.
fileno
())
...
...
@@ -44,7 +45,8 @@ def popen_communicate(args, data=''):
if
not
chunk
:
break
chunks
.
append
(
chunk
)
except
IOError
,
ex
:
except
IOError
:
ex
=
sys
.
exc_info
()[
1
]
if
ex
[
0
]
!=
errno
.
EAGAIN
:
raise
sys
.
exc_clear
()
...
...
@@ -64,10 +66,10 @@ if __name__ == '__main__':
# print the results (if available)
if
job1
.
ready
():
print
'finger: %s bytes: %s'
%
(
len
(
job1
.
value
or
''
),
repr
(
job1
.
value
)[:
50
]
)
print
(
'finger: %s bytes: %s'
%
(
len
(
job1
.
value
or
''
),
repr
(
job1
.
value
)[:
50
])
)
else
:
print
'finger: job is still running'
print
(
'finger: job is still running'
)
if
job2
.
ready
():
print
'netstat: %s bytes: %s'
%
(
len
(
job2
.
value
or
''
),
repr
(
job2
.
value
)[:
50
]
)
print
(
'netstat: %s bytes: %s'
%
(
len
(
job2
.
value
or
''
),
repr
(
job2
.
value
)[:
50
])
)
else
:
print
'netstat: job is still running'
print
(
'netstat: job is still running'
)
examples/webproxy.py
View file @
42a56843
...
...
@@ -51,13 +51,14 @@ def proxy(path, start_response, proxy_url):
try
:
try
:
response
=
urllib2
.
urlopen
(
path
)
except
urllib2
.
HTTPError
,
ex
:
response
=
ex
print
'%s: %s %s'
%
(
path
,
response
.
code
,
response
.
msg
)
except
urllib2
.
HTTPError
:
response
=
sys
.
exc_info
()[
1
]
print
(
'%s: %s %s'
%
(
path
,
response
.
code
,
response
.
msg
)
)
headers
=
[(
k
,
v
)
for
(
k
,
v
)
in
response
.
headers
.
items
()
if
k
not
in
drop_headers
]
scheme
,
netloc
,
path
,
params
,
query
,
fragment
=
urlparse
(
path
)
host
=
(
scheme
or
'http'
)
+
'://'
+
netloc
except
Exception
,
ex
:
except
Exception
:
ex
=
sys
.
exc_info
()[
1
]
sys
.
stderr
.
write
(
'error while reading %s:
\
n
'
%
path
)
traceback
.
print_exc
()
tb
=
traceback
.
format_exc
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment