Utilisation des WebSockets en python

Voici un exemple de code permettant une diffusion de message avec les WebSocket.
La démonstration est composé de 3 programmes:

  • Le serveur WebSocket en python qui accepte les connexion
  • Un client WebSocket en python qui emet des messages
  • Un client WebSocket en Javascipt dans une page html qui affiche les messages

Les clients se connectent sur les différentes URL:

  • ws://127.0.0.1:5678/broadcast/read : Pour lire les messages diffusés
  • ws://127.0.0.1:5678/broadcast/write : Pour diffuser un message

Le serveur:

#!/usr/bin/env python3

import asyncio
import datetime
import random
import websockets

connected = set()

async def pub_sub(websocket, path):
    global connected
    if path == '/broadcast/read' :        
        connected.add(websocket)
        print("READER "+str(websocket.remote_address)+"    connected")
        while True:
            await asyncio.sleep(100)
    elif path == '/broadcast/write' :
        print("WRITER "+str(websocket.remote_address)+"    connected")
        try :
            while True:
                data = await websocket.recv()
                print("MULTICAST: "+data)
                still_connected = set()
                for ws in connected :
                    if ws.open:
                        still_connected.add(ws)
                        await asyncio.wait([ws.send(data)])
                    else:
                        print("READER "+str(ws.remote_address)+" disconnected")
                connected=still_connected
        except:
            print("WRITER "+str(websocket.remote_address)+" disconnected")
            
start_server = websockets.serve(pub_sub, '127.0.0.1', 5678)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

Le client python qui emet des messages

#!/usr/bin/env python3

import asyncio
import websockets

async def hello():
    async with websockets.connect('ws://localhost:5678/broadcast/write') as websocket:
        while True:
            name = input("Message ? ")
            await websocket.send(name)

asyncio.get_event_loop().run_until_complete(hello())

La ou les pages html qui recoivent les messages:

<!DOCTYPE html>
<html>
    <head>
        <title>WebSocket demo</title>
    </head>
    <body>
        <script>
            var ws = new WebSocket("ws://127.0.0.1:5678/broadcast/read"),
                messages = document.createElement('ul');
            ws.onmessage = function (event) {
                var messages = document.getElementsByTagName('ul')[0],
                    message = document.createElement('li'),
                    content = document.createTextNode(event.data);
                message.appendChild(content);
                messages.appendChild(message);
            };
            document.body.appendChild(messages);
        </script>
    </body>
</html>

Oracle Storage Cloud Service: Creating Containers Using the REST API

Here an interactive script to create a storage container:
Download here

#!/bin/bash

## DESCRIPTION: Oracle Storage Cloud Service: Creating Containers Using the REST API
## DOCUMENTATION: http://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/objectstorage/creating_containers_REST_API/creating_containers_REST_API.html

## KNOWN ISSUES: Does not work with the following identity domains:
## - oucloudusa8

## AUTHOR: Sebastien Colas 


echo -n 'Your identity domain :'
read identity
echo -n 'Your username :'
read username
echo -n 'Your password :'
read password
echo -n 'Container name you want to create :'
read container

echo

curl -v -s -X GET -H "X-Storage-User: Storage-${identity}:${username}" -H "X-Storage-Pass: ${password}" https://${identity}.storage.oraclecloud.com/auth/v1.0 2> auth.txt

storage=`cat auth.txt | grep X-Storage-Url auth.txt | cut -f 3 -d ' ' | col -bp`
auth=`cat auth.txt | grep X-Auth-Token | cut -f 3 -d ' '`

echo "X-Storage-Url : ${storage}"
echo "X-Auth-Token : ${auth}"

curl -v -s -X PUT -H "X-Auth-Token: ${auth}" ${storage}/${container} 2> create.txt

ret=`cat create.txt | grep 'HTTP/1.1 201 Created'`

echo

if [ -n "$ret" ]
then 
  echo "Container ${container} successfully created"
  echo "Cloud Storage Container: Storage-${identity}/${container}"
else
  echo "an error occured please see the log files"
fi

Get number of unread posts on phpbb3 in php

<?php

/*
* Doc: https://www.phpbb.com/customise/db/mod/view_or_mark_unread_posts/support/unread_posts_counter_small_optimize-t_99119
*/

/* Headers JSON */
header('Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . 'GMT' );
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: application/json; charset=iso-8859-1');

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../forum/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include ($phpbb_root_path . 'common.' . $phpEx);

$user->session_begin();

global $db, $user;

if ($user->data['user_id'] == ANONYMOUS)
{
echo '{"unread_posts":"0"}';
return;
}

// Select unread topics
$unread_topics = array ();
$sql = 'SELECT t.topic_id
FROM ' . TOPICS_TABLE . ' t
LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (tt.user_id = ' . $user->data['user_id'] . ' AND t.topic_id = tt.topic_id)
LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (ft.user_id = ' . $user->data['user_id'] . ' AND t.forum_id = ft.forum_id)
WHERE t.topic_last_post_time > ' . $user->data['user_lastmark'] . ' AND
(
(tt.mark_time IS NOT NULL AND t.topic_last_post_time > tt.mark_time) OR
(tt.mark_time IS NULL AND ft.mark_time IS NOT NULL AND t.topic_last_post_time > ft.mark_time) OR
(tt.mark_time IS NULL AND ft.mark_time IS NULL)
)
LIMIT 1001';

$result = $db->;sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$unread_topics[] = $row['topic_id'];
}

if (empty ($unread_topics))
{
echo '{"unread_posts":"0"}';
return;
}

// now count the posts with post time after each of the relevant times
$sql = 'SELECT COUNT(p.post_id) as count
FROM ' . POSTS_TABLE . ' p
LEFT JOIN ' . FORUMS_TRACK_TABLE . ' ft ON (p.forum_id = ft.forum_id AND ft.user_id = ' . $user->data['user_id'] . ')
LEFT JOIN ' . TOPICS_TRACK_TABLE . ' tt ON (p.topic_id = tt.topic_id AND tt.user_id = ' . $user->data['user_id'] . ')
WHERE ' . $db->sql_in_set('p.topic_id', $unread_topics) . '
AND
(
p.post_time > tt.mark_time
OR (tt.mark_time IS NULL AND p.post_time > ft.mark_time)
OR (ft.mark_time IS NULL AND p.post_time > ' . $user->data['user_lastmark'] . ')
)';

$result = $db->sql_query($sql);
$unread_post_count = $db->sql_fetchfield('count', false, $result);
$db->sql_freeresult($result);

echo '{"unread_posts":"' . $unread_post_count . '"}';

?>

Serveur VNC de BrickPi

Pour changer la résolution du serveur VNC de BrickPi il suffit d’éditer le fichier

/etc/systemd/system/vncserver@.service

Ci dessous je change la résolution en 1920×180:
[Unit]
Description=Remote desktop service (VNC)
After=syslog.target network.target
[Service]
Type=forking
User=pi
PAMName=login
PIDFile=/home/pi/.vnc/%H:%i.pid
WorkingDirectory=/home/pi
ExecStartPre=-/usr/bin/vncserver -kill :%i
ExecStart=/usr/bin/vncserver -depth 24 -geometry 1920×1080 :%i
ExecStop=/usr/bin/vncserver -kill :%i
[Install]
WantedBy=multi-user.target

Ensuite on redémarre et voilà le travail:

root@brickpi:/home/pi# systemctl restart vncserver@1.service

Mise à jour de WordPress sur free.fr

Free ne supportant qu’un vieille version de PHP il m’était alors impossible de mettre à jour WordPress.
Avec le support de PHP 5.6.8 la mise à jour de WordPress est désormais possible.
Du coup le site est désormais sur la version 4.5.2–fr_FR de WordPress.
Coté technique il suffit de créé un fichier .htaccess contenant les informations suivantes:

<IfDefine Free>
php56 1
</IfDefine>

Oracle Traffic Director UNIX init script

Oracle does not provite /etc/init.d script to automatically launch Oracle Traffic Director at unix/linux startup. Here my own script:

#!/bin/bash
#
# oracle-traffic-director	This shell script takes care of starting and stopping
#				oracle-traffic-director.
#
# chkconfig: 345 99 0
# description: Start/Stop Oracle Traffic Director

# Oracle Traffic Director Instance
OTD_INSTANCE_DIRECTORY=/u01/app/oracle/product/11.1.1.7.0/trafficdirector_instance
OTD_INSTANCE=net-demo1

# Source function library.
. /etc/init.d/functions

start() {
        # Start daemons.
        echo -n $"Starting Oracle Traffic Director Instance $OTD_INSTANCE: "
        daemon ${OTD_INSTANCE_DIRECTORY}/${OTD_INSTANCE}/bin/startserv
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ]
	return $RETVAL
}

stop() {
        echo -n $"Shutting down Oracle Traffic Director Instance $OTD_INSTANCE: "
        daemon ${OTD_INSTANCE_DIRECTORY}/${OTD_INSTANCE}/bin/stopserv
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ]
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 2
esac