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
e6022b66
Commit
e6022b66
authored
Apr 22, 2010
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite examples/echoserver.py using StreamServer class
parent
7d7b0574
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
40 deletions
+22
-40
examples/echoserver.py
examples/echoserver.py
+22
-40
No files found.
examples/echoserver.py
View file @
e6022b66
#! /usr/bin/env python
#! /usr/bin/env python
"""Simple server that listens on port 6000 and echos back every input to the client.
# Copyright (c) 2007, Linden Research, Inc.
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""Simple server that listens on port 6000 and echos back every input to
the client. To try out the server, start it up by running this file.
Connect to it with:
Connect to it with:
telnet localhost 6000
telnet localhost 6000
You terminate your connection by terminating telnet (typically Ctrl-]
Terminate the connection by terminating telnet (typically Ctrl-] and then 'quit').
and then 'quit')
"""
"""
import
gevent
from
gevent.server
import
StreamServer
from
gevent
import
socket
def
handle_socket
(
fileobj
):
# this handler will be run for each incoming connection in a dedicated greenlet
def
echo
(
socket
,
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
'
)
fileobj
.
flush
()
while
True
:
while
True
:
line
=
fileobj
.
readline
()
line
=
fileobj
.
readline
()
if
not
line
:
if
not
line
:
print
"client disconnected"
break
if
line
.
strip
().
lower
()
==
'quit'
:
print
"client quit"
break
break
fileobj
.
write
(
line
)
fileobj
.
write
(
line
)
fileobj
.
flush
()
fileobj
.
flush
()
print
"echoed"
,
repr
(
line
)
print
"echoed"
,
repr
(
line
)
print
"client disconnected"
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
"server socket listening on port 6000"
# to make the server use SSL, pass certfile and keyfile arguments to the constructor
server
=
socket
.
socket
()
server
=
StreamServer
((
'0.0.0.0'
,
6000
),
echo
)
server
.
bind
((
'0.0.0.0'
,
6000
))
# to start the server asynchronously, use its start() method;
server
.
listen
(
5
)
# we use blocking serve_forever() here because we have no other jobs
while
True
:
print
'Starting echo server on port 6000'
try
:
server
.
serve_forever
()
new_sock
,
address
=
server
.
accept
()
except
KeyboardInterrupt
:
break
# handle every new connection with a new coroutine
gevent
.
spawn
(
handle_socket
,
new_sock
.
makefile
())
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