Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
noVNC
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
rasky
noVNC
Commits
033df68d
Commit
033df68d
authored
Mar 31, 2010
by
Joel Martin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simpler non-threaded ws_echo.py
parent
55055896
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
60 deletions
+37
-60
ws_echo.py
ws_echo.py
+37
-60
No files found.
ws_echo.py
View file @
033df68d
#!/usr/bin/python
#!/usr/bin/python
# File: asynchat-example-1.py
import
asyncore
,
asynchat
import
sys
,
os
,
socket
import
sys
,
os
,
socket
,
string
server_handshake
=
"""HTTP/1.1 101 Web Socket Protocol Handshake
\r
server_handshake
=
"""HTTP/1.1 101 Web Socket Protocol Handshake
\r
Upgrade: WebSocket
\r
Upgrade: WebSocket
\r
...
@@ -13,66 +11,45 @@ WebSocket-Protocol: sample\r
...
@@ -13,66 +11,45 @@ WebSocket-Protocol: sample\r
\r
\r
"""
"""
class
WSChannel
(
asynchat
.
async_chat
):
def
start_server
(
port
):
tick
=
0
def
__init__
(
self
,
server
,
sock
,
addr
):
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
print
">> WSChannel.__init__"
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
asynchat
.
async_chat
.
__init__
(
self
,
sock
)
sock
.
bind
((
''
,
port
))
self
.
set_terminator
(
"
\r\n\r\n
"
)
sock
.
listen
(
100
)
self
.
handshake
=
None
while
True
:
self
.
data
=
""
try
:
self
.
shutdown
=
0
print
'listening on port
%
s'
%
port
csock
,
address
=
sock
.
accept
()
def
collect_incoming_data
(
self
,
data
):
tick
+=
1
#print ">> WSChannel.collect_incoming_data"
print
'Got connection'
self
.
data
=
self
.
data
+
data
handshake
(
csock
,
tick
)
print
'handshaken'
def
found_terminator
(
self
):
while
True
:
#print ">> WSChannel.found_terminator"
interact
(
csock
,
tick
)
if
not
self
.
handshake
:
tick
+=
1
# got the client handshake lines
except
Exception
,
e
:
self
.
handshake
=
self
.
data
print
"Ignoring exception:"
,
e
req_lines
=
self
.
handshake
.
split
(
"
\r\n
"
)
_
,
path
,
_
=
req_lines
[
0
]
.
split
(
" "
)
def
handshake
(
client
,
tick
):
_
,
origin
=
req_lines
[
4
]
.
split
(
" "
)
handshake
=
client
.
recv
(
255
)
_
,
host
=
req_lines
[
3
]
.
split
(
" "
)
req_lines
=
handshake
.
split
(
"
\r\n
"
)
print
"*** got handshake:
\n
%
s"
%
self
.
handshake
_
,
path
,
_
=
req_lines
[
0
]
.
split
(
" "
)
print
"*** origin:
%
s, location: ws://
%
s
%
s"
%
(
origin
,
host
,
path
)
_
,
origin
=
req_lines
[
4
]
.
split
(
" "
)
self
.
push
(
server_handshake
%
(
origin
,
host
,
path
))
_
,
host
=
req_lines
[
3
]
.
split
(
" "
)
# self.push("HTTP/1.1 101 Web Socket Protocol Handshake\r\n")
print
"*** got handshake:
\n
%
s"
%
handshake
# self.push("Upgrade: WebSocket\r\n")
print
"*** origin:
%
s, location: ws://
%
s
%
s"
%
(
origin
,
host
,
path
)
# self.push("Connection: Upgrade\r\n")
client
.
send
(
server_handshake
%
(
origin
,
host
,
path
))
# self.push("WebSocket-Origin: %s\r\n" % origin)
# self.push("WebSocket-Location: ws://%s%s\r\n" % (host, path))
def
interact
(
client
,
tick
):
# self.push("WebSocket-Protocol: sample\r\n")
data
=
client
.
recv
(
255
)
# self.push("\r\n")
print
'got:
%
s'
%
(
data
)
self
.
set_terminator
(
"
\xff
"
)
# look for frame terminators
client
.
send
(
"
\x00
server response
%
d
\xff
"
%
(
tick
))
else
:
# return payload.
print
"received:
%
s"
%
self
.
data
self
.
push
(
"
\x00
client sent:
%
s
\xff
"
%
self
.
data
)
self
.
data
=
""
class
WSServer
(
asyncore
.
dispatcher
):
def
__init__
(
self
,
port
):
asyncore
.
dispatcher
.
__init__
(
self
)
self
.
create_socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
bind
((
""
,
port
))
self
.
listen
(
5
)
print
"<< WSServer.__init__"
def
handle_accept
(
self
):
print
">> WSServer.handle_accept"
conn
,
addr
=
self
.
accept
()
WSChannel
(
self
,
conn
,
addr
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
if
len
(
sys
.
argv
)
<
2
:
if
len
(
sys
.
argv
)
<
2
:
print
"Usage:
%
s <port>"
%
sys
.
argv
[
0
]
print
"Usage:
%
s <port>"
%
sys
.
argv
[
0
]
sys
.
exit
(
2
)
sys
.
exit
(
2
)
PORT
=
int
(
sys
.
argv
[
1
])
PORT
=
int
(
sys
.
argv
[
1
])
s
=
WSServer
(
PORT
)
start_server
(
PORT
)
print
"serving Web Socket at port"
,
PORT
,
"..."
asyncore
.
loop
()
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