<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.randomness.nu/mediawiki/index.php?action=history&amp;feed=atom&amp;title=websred.py</id>
	<title>websred.py - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.randomness.nu/mediawiki/index.php?action=history&amp;feed=atom&amp;title=websred.py"/>
	<link rel="alternate" type="text/html" href="https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;action=history"/>
	<updated>2026-05-24T23:59:24Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=66&amp;oldid=prev</id>
		<title>Derg: Derg moved page Websred.py to websred.py without leaving a redirect</title>
		<link rel="alternate" type="text/html" href="https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=66&amp;oldid=prev"/>
		<updated>2024-09-12T12:50:05Z</updated>

		<summary type="html">&lt;p&gt;Derg moved page &lt;a href=&quot;/mediawiki/index.php?title=Websred.py&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Websred.py (page does not exist)&quot;&gt;Websred.py&lt;/a&gt; to &lt;a href=&quot;/websred.py&quot; title=&quot;websred.py&quot;&gt;websred.py&lt;/a&gt; without leaving a redirect&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 14:50, 12 September 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Derg</name></author>
	</entry>
	<entry>
		<id>https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=15&amp;oldid=prev</id>
		<title>Derg at 19:45, 9 September 2024</title>
		<link rel="alternate" type="text/html" href="https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=15&amp;oldid=prev"/>
		<updated>2024-09-09T19:45:39Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 21:45, 9 September 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l2&quot;&gt;Line 2:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 2:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;!/usr/bin/python&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;!/usr/bin/python&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;import argparse&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;import argparse&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;parser = argparse.ArgumentParser()&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;parser = argparse.ArgumentParser()&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Derg</name></author>
	</entry>
	<entry>
		<id>https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=14&amp;oldid=prev</id>
		<title>Derg: Created page with &quot;&lt;syntaxhighlight lang=&quot;python&quot;&gt; !/usr/bin/python import argparse   parser = argparse.ArgumentParser() parser.add_argument(&#039;-d&#039;, &#039;--daemonize&#039;, dest=&#039;daemon&#039;, action=&#039;store_true&#039;) parser.add_argument(&#039;-p&#039;, &#039;--port&#039;,      dest=&#039;port&#039;, type=int, default=8079) parser.add_argument(&#039;-i&#039;, &#039;--subscribe&#039;, dest=&#039;subscribe&#039;) parser.add_argument(&#039;-o&#039;, &#039;--publish&#039;,   dest=&#039;publish&#039;) parser.add_argument(&#039;-v&#039;, &#039;--debug&#039;,     dest=&#039;debug&#039;, action=&#039;store_true&#039;) parser.add_argument(&#039;-l&#039;,...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.randomness.nu/mediawiki/index.php?title=websred.py&amp;diff=14&amp;oldid=prev"/>
		<updated>2024-09-09T19:45:19Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; !/usr/bin/python import argparse   parser = argparse.ArgumentParser() parser.add_argument(&amp;#039;-d&amp;#039;, &amp;#039;--daemonize&amp;#039;, dest=&amp;#039;daemon&amp;#039;, action=&amp;#039;store_true&amp;#039;) parser.add_argument(&amp;#039;-p&amp;#039;, &amp;#039;--port&amp;#039;,      dest=&amp;#039;port&amp;#039;, type=int, default=8079) parser.add_argument(&amp;#039;-i&amp;#039;, &amp;#039;--subscribe&amp;#039;, dest=&amp;#039;subscribe&amp;#039;) parser.add_argument(&amp;#039;-o&amp;#039;, &amp;#039;--publish&amp;#039;,   dest=&amp;#039;publish&amp;#039;) parser.add_argument(&amp;#039;-v&amp;#039;, &amp;#039;--debug&amp;#039;,     dest=&amp;#039;debug&amp;#039;, action=&amp;#039;store_true&amp;#039;) parser.add_argument(&amp;#039;-l&amp;#039;,...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
!/usr/bin/python&lt;br /&gt;
import argparse&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
parser = argparse.ArgumentParser()&lt;br /&gt;
parser.add_argument(&amp;#039;-d&amp;#039;, &amp;#039;--daemonize&amp;#039;, dest=&amp;#039;daemon&amp;#039;, action=&amp;#039;store_true&amp;#039;)&lt;br /&gt;
parser.add_argument(&amp;#039;-p&amp;#039;, &amp;#039;--port&amp;#039;,      dest=&amp;#039;port&amp;#039;, type=int, default=8079)&lt;br /&gt;
parser.add_argument(&amp;#039;-i&amp;#039;, &amp;#039;--subscribe&amp;#039;, dest=&amp;#039;subscribe&amp;#039;)&lt;br /&gt;
parser.add_argument(&amp;#039;-o&amp;#039;, &amp;#039;--publish&amp;#039;,   dest=&amp;#039;publish&amp;#039;)&lt;br /&gt;
parser.add_argument(&amp;#039;-v&amp;#039;, &amp;#039;--debug&amp;#039;,     dest=&amp;#039;debug&amp;#039;, action=&amp;#039;store_true&amp;#039;)&lt;br /&gt;
parser.add_argument(&amp;#039;-l&amp;#039;, &amp;#039;--log&amp;#039;,       dest=&amp;#039;log&amp;#039;, action=&amp;#039;store_true&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
if args.log:&lt;br /&gt;
    import sys&lt;br /&gt;
    from twisted.python import log&lt;br /&gt;
    log.startLogging(sys.stdout)&lt;br /&gt;
&lt;br /&gt;
from twisted.internet import reactor, protocol, defer&lt;br /&gt;
from txws import WebSocketFactory&lt;br /&gt;
import txredisapi as redis&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class MyWebSocketProtocol(protocol.Protocol):&lt;br /&gt;
&lt;br /&gt;
    def connectionLost(self,reason=None):&lt;br /&gt;
        del self.factory.clients[self.transport.getPeer()]&lt;br /&gt;
&lt;br /&gt;
    def dataReceived(self, msg):&lt;br /&gt;
        if args.debug: print(&amp;#039;WEB&amp;#039;, self.transport.getPeer(), msg)&lt;br /&gt;
        bridge.webtoredis(msg)&lt;br /&gt;
&lt;br /&gt;
class MyWebSocketFactory(protocol.Factory):&lt;br /&gt;
    clients = {}&lt;br /&gt;
&lt;br /&gt;
    def __init__(self):&lt;br /&gt;
        bridge.web = self&lt;br /&gt;
&lt;br /&gt;
    def buildProtocol(self, addr):&lt;br /&gt;
      if args.debug: print(&amp;#039;%s connected to web socket&amp;#039; % addr)&lt;br /&gt;
      a = MyWebSocketProtocol()&lt;br /&gt;
      a.factory = self&lt;br /&gt;
      self.clients[addr] = a&lt;br /&gt;
      return a&lt;br /&gt;
&lt;br /&gt;
    def sendToAll(self, msg):&lt;br /&gt;
      for a in self.clients.values():&lt;br /&gt;
          a.transport.write(msg)&lt;br /&gt;
&lt;br /&gt;
class MyRedisProtocol(redis.SubscriberProtocol):&lt;br /&gt;
    def connectionMade(self):&lt;br /&gt;
        if args.debug: print(&amp;#039;Connected to Redis subscriber&amp;#039;)&lt;br /&gt;
        self.subscribe(args.subscribe)&lt;br /&gt;
&lt;br /&gt;
    def messageReceived(self, channel, pattern, message):&lt;br /&gt;
        message = str(message)&lt;br /&gt;
        if args.debug: print(&amp;#039;RED&amp;#039;, message)&lt;br /&gt;
        bridge.redistoweb(message)&lt;br /&gt;
&lt;br /&gt;
    def connectionLost(self, reason):&lt;br /&gt;
        pass #print &amp;quot;lost connection:&amp;quot;, reason&lt;br /&gt;
&lt;br /&gt;
class MyRedisFactory(redis.SubscriberFactory):&lt;br /&gt;
&lt;br /&gt;
    maxDelay = 120&lt;br /&gt;
    continueTrying = True&lt;br /&gt;
    protocol = MyRedisProtocol&lt;br /&gt;
&lt;br /&gt;
class Bridge:&lt;br /&gt;
    web = None&lt;br /&gt;
    red = None&lt;br /&gt;
&lt;br /&gt;
    def redistoweb(self, message):&lt;br /&gt;
        if not self.web: return&lt;br /&gt;
        self.web.sendToAll(message)&lt;br /&gt;
&lt;br /&gt;
    def webtoredis(self, message):&lt;br /&gt;
        if not self.red: return&lt;br /&gt;
        self.red.publish(args.publish, message)&lt;br /&gt;
&lt;br /&gt;
def fork(func):&lt;br /&gt;
    import os&lt;br /&gt;
    pid = os.fork()&lt;br /&gt;
    if pid &amp;gt; 0: return&lt;br /&gt;
    os.chdir(&amp;quot;/&amp;quot;)&lt;br /&gt;
    os.setsid()&lt;br /&gt;
    os.umask(0)&lt;br /&gt;
    pid = os.fork()&lt;br /&gt;
    if pid &amp;gt; 0: return&lt;br /&gt;
    func()&lt;br /&gt;
    os._exit(os.EX_OK)&lt;br /&gt;
&lt;br /&gt;
@defer.inlineCallbacks&lt;br /&gt;
def redconnect():&lt;br /&gt;
    red = yield redis.Connection(&amp;#039;localhost&amp;#039;, 6379)&lt;br /&gt;
    if args.debug: print(&amp;#039;Connected to Redis&amp;#039;)&lt;br /&gt;
    bridge.red = red&lt;br /&gt;
&lt;br /&gt;
bridge = Bridge()&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    reactor.listenTCP(args.port, WebSocketFactory(MyWebSocketFactory()))&lt;br /&gt;
    if args.subscribe:&lt;br /&gt;
        reactor.connectTCP(&amp;quot;localhost&amp;quot;, 6379, MyRedisFactory())&lt;br /&gt;
    if args.publish:&lt;br /&gt;
        reactor.callWhenRunning(redconnect)&lt;br /&gt;
    reactor.run()&lt;br /&gt;
&lt;br /&gt;
if args.daemon:&lt;br /&gt;
    fork(main)&lt;br /&gt;
else:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Derg</name></author>
	</entry>
</feed>