1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#!/usr/bin/python3
# vim: set fileencoding=utf-8 foldmethod=marker :
# {{{ Copyright 2013-2023 Bas Wijnen <wijnen@debian.org>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# }}}
import time
import websocketd
class Rpc:
'An object of this class is instanced for every accepted WebSocket.'
def __init__(self, remote):
'remote is an object like this one, to call remote functions on.'
self.remote = remote
# Call some functions.
# This waits for the client to complete the function.
self.remote.foo()
# This sends the event, but does not wait for completion. The
# return value is lost.
self.remote.foo.event()
# This is the same as event(), but handles the return value in a callback.
self.remote.foo.bg(lambda ret: print('foo returned %s' % repr(ret)))
# Arguments can also be passed.
self.remote.bar(25)
def baz(self, arg1, arg2 = 42):
'This function is called from the client.'
print('baz called with {}, {}'.format(arg1, arg2))
# Return a value.
return 'a value'
def bam(self):
'''A generator can yield to free the processor. It will return
at a later time.
It needs to make sure that it continues running. The initial
statement must be a yield, which will return immediately with
its wake function. The wake function must be passed to the code
that will wake up the generator.'''
wake = (yield)
print('running blocking function')
# Schedule a timeout 3 seconds from now and use wake as the callback function.
websocketd.add_timeout(time.time() + 3, wake)
# Wait for the timeout.
yield
# Return a value.
return 'Ready!'
def quit(self):
'This function is called from the client.'
print('quit called, stopping main loop.')
websocketd.endloop()
# Run the server on port 1234, serving html pages from the directory named html.
# The Rpc class is used for handling WebSockets.
# Note that the server defaults to https connections; for this example,
# encryptions is not used. This is done by adding 'tls = False' as an
# argument.
# In normal use, encryption should almost always be used. The network module
# automatically generates an encryption key for you and puts it in
# ~/.local/share/network/. If you need a certificate for your encryption key,
# see http://letsencrypt.org.
# Alternatively, if you use Apache as a virtual proxy, it can do all the
# encryption for you.
server = websocketd.RPChttpd(1234, Rpc, httpdirs = ('html',), tls = False)
# Tell the user that things are set up.
websocketd.log('running on port 1234')
# Run the main loop.
websocketd.fgloop()
# This is reached when the mail loop is stopped.
print('End of program.')
|