Python 3: simple JSON TCP server and client

February 7th, 2013 No comments

upon request, ported my old python 2 json server example to python 3:

server.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import socketserver
import json

class MyTCPServer(socketserver.ThreadingTCPServer):
    allow_reuse_address = True

class MyTCPServerHandler(socketserver.BaseRequestHandler):
    def handle(self):
        try:
            data = json.loads(self.request.recv(1024).decode('UTF-8').strip())
            # process the data, i.e. print it:
            print(data)
            # send some 'ok' back
            self.request.sendall(bytes(json.dumps({'return':'ok'}), 'UTF-8'))
        except Exception as e:
            print("Exception wile receiving message: ", e)

server = MyTCPServer(('127.0.0.1', 13373), MyTCPServerHandler)
server.serve_forever()

client.py:

1
2
3
4
5
6
7
8
9
10
11
import socket
import json

data = {'message':'hello world!', 'test':123.4}

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 13373))
s.send(bytes(json.dumps(data), 'UTF-8'))
result = json.loads(s.recv(1024).decode('UTF-8'))
print(result)
s.close()
Categories: coding, python Tags:

An interactive LUA console … in LUA

October 7th, 2012 No comments

simple and handy for embedded systems

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function startConsole()
    while true
    do
        io.write("> ")
        io.flush()
        line = io.read()
        if(line == "exit" or line == "quit") then break end
       
        local func, err  = loadstring(line)
        if func then
            local ok, result = xpcall(func, debug.traceback)
            if not ok then
                print("Error: "..result)
            end
        else
            print("Error: "..err)
        end
    end
end

startConsole()
Categories: coding, lua Tags:

Python: IRC client in 19 lines

August 27th, 2012 No comments

so, very, very condensed example (see this as well):

UPDATE: thanks to estama, now with proper socket line reading :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import socket
s = socket.socket()
s.connect(('irc.rigsofrods.org', 6667))
s.send("NICK F00bar\r\n")
s.send("USER F00bar F00bar F00bar :F00bar\r\n")
connected = False
f = s.makefile('r', 4096)
while True:
    data = f.readline().strip()
    print data
    if data.find('PING') != -1:
        s.send('PONG :' + data.split(':')[1])
        if not connected:
            connected = True
            s.send("PRIVMSG R : Login <>")
            s.send("MODE F00bar +x")
            s.send("JOIN #bla")
            s.send('PRIVMSG #bla hello world!')
f.close()
Categories: coding, python Tags:

Python: Roll your own IRC client in under 80 Lines

August 27th, 2012 4 comments

Its actually quite easy, as the IRC protocol is in plain text and not too complicated. Example Implementation:

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
import socket

class IRCClient:
    socket = None
    connected = False
    nickname = 'RowBot'
    channels = ['#foobar', '#barefoot']

    def __init__(self):
        self.socket = socket.socket()
        self.socket.connect(('irc.rigsofrods.org', 6667))
        self.send("NICK %s" % self.nickname)
        self.send("USER %(nick)s %(nick)s %(nick)s :%(nick)s" % {'nick':self.nickname})

        while True:
            buf = self.socket.recv(4096)
            lines = buf.split("\n")
            for data in lines:
                data = str(data).strip()

                if data == '':
                    continue
                print "I<", data

                # server ping/pong?
                if data.find('PING') != -1:
                    n = data.split(':')[1]
                    self.send('PONG :' + n)
                    if self.connected == False:
                        self.perform()
                        self.connected = True

                args = data.split(None, 3)
                if len(args) != 4:
                    continue
                ctx = {}
                ctx['sender'] = args[0][1:]
                ctx['type']   = args[1]
                ctx['target'] = args[2]
                ctx['msg']    = args[3][1:]

                # whom to reply?
                target = ctx['target']
                if ctx['target'] == self.nickname:
                    target = ctx['sender'].split("!")[0]

                # some basic commands
                if ctx['msg'] == '!help':
                    self.say('available commands: !help', target)

                # directed to the bot?
                if ctx['type'] == 'PRIVMSG' and (ctx['msg'].lower()[0:len(self.nickname)] == self.nickname.lower() or ctx['target'] == self.nickname):
                    # something is speaking to the bot
                    query = ctx['msg']
                    if ctx['target'] != self.nickname:
                        query = query[len(self.nickname):]
                        query = query.lstrip(':,;. ')
                    # do something intelligent here, like query a chatterbot
                    print 'someone spoke to us: ', query
                    self.say('alright :|', target)

    def send(self, msg):
        print "I>",msg
        self.socket.send(msg+"\r\n")

    def say(self, msg, to):
        self.send("PRIVMSG %s :%s" % (to, msg))

    def perform(self):
        #self.send("PRIVMSG R : Register <>")
        self.send("PRIVMSG R : Login <>")
        self.send("MODE %s +x" % self.nickname)
        for c in self.channels:
            self.send("JOIN %s" % c)
            # say hello to every channel
            self.say('hello world!', c)

IRCClient()

This is a stripped down version with no error checking or exception handling to come to the point.

Categories: coding, python Tags:

Python: simple JSON TCP server and client

August 27th, 2012 6 comments

So, you want to exchange data over the network between two python programs in JSON format? Or just send or receive data in JSON? No problem:

server.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import SocketServer
import json

class MyTCPServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

class MyTCPServerHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        try:
            data = json.loads(self.request.recv(1024).strip())
            # process the data, i.e. print it:
            print data
            # send some 'ok' back
            self.request.sendall(json.dumps({'return':'ok'}))
        except Exception, e:
            print "Exception wile receiving message: ", e

server = MyTCPServer(('127.0.0.1', 13373), MyTCPServerHandler)
server.serve_forever()

client.py:

1
2
3
4
5
6
7
8
9
10
11
import socket
import json

data = {'message':'hello world!', 'test':123.4}

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 13373))
s.send(json.dumps(data))
result = json.loads(s.recv(1024))
print result
s.close()

simple, eh? ;)

Categories: coding, python Tags:

Python: Proxy Object around dicts, or: how to clean up code a bit

August 26th, 2012 No comments

Imagine, you have a huge python dict that you want to access. Usually it ends up looking like this:

1
2
3
4
d = {"foo":"bar","key":"value"}

print d['foo']
print d['key']

So, if you query that dict lots of times, the [''] will kind of get in the way for readability. So the solution could be using this wrapper class:

1
2
3
4
5
6
7
8
9
10
11
12
13
class DictObjectProxy():
    obj = None
    def __init__(self, obj):
        self.obj = obj
    def __getattr__(self, name):
        if not name in self.obj:
            return ""
        return self.obj[name]

d = {"foo":"bar","key":"value"}
d = DictObjectProxy(ar)
print d.foo
print d.key

looks far more clean IMHO :)

(also, take care of what happens when they key is not known)

Categories: coding, python Tags:

HowTo: new debian squeeze VM for XEN (the manual way)

August 24th, 2012 No comments

How to set up debian squeeze as domU. Replace irc with the desired name

execute on dom0:

1
2
3
4
5
6
7
8
mkdir /mnt/target
lvcreate -L 20G vg -nirc
lvcreate -L 2G vg -nirc_swap
mkfs.ext3 /dev/vg/irc
mkswap /dev/vg/irc_swap
mount /dev/vg/irc /mnt/target
cd /mnt/target
debootstrap squeeze . http://ftp.us.debian.org/debian

configuring:

1
2
3
4
vi etc/fstab
vi etc/network/interfaces
chroot . /bin/bash
passwd

basic installation fixup:

1
2
3
4
apt-get remove vim-tiny
apt-get install vim ctags vim-doc vim-scripts
exit
umount /mnt/target
1
vi /etc/xen/irc

(fix up before pasting!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#  -*- mode: python; -*-
kernel   = "/boot/vmlinuz-X-xen-amd64"
ramdisk  = "/boot/initrd.img-X-xen-amd64"

memory   = 512
name     = 'irc'

hostname = name + ".yourdomain.com"

vif = [
        'ip=<IPV6> <IPV4>,mac=<MAC>',
        'ip=<IPV4_internal>,mac=<MAC>'
      ]

disk = [
        'phy:/dev/vg/'+name+'     ,sda1,w',
        'phy:/dev/vg/'+name+'_swap,sda2,w'
       ]

root     = "/dev/sda1 ro"

vcpus = "1"
cpus="3"
extra = "xencons=tty console=xvc0 barrier=0 clocksource=jiffies"

boot the new machine:

1
2
3
4
5
6
xm create -c irc
#after logging in:
vi /etc/hostname
hostname <>
apt-get install openssh-server
# install more software now :)

now how to install qwebirc, in case you want that ;)

1
2
3
4
5
6
7
8
9
10
apt-get install python python-twisted python-twisted-bin python-twisted-core python-twisted-runner python-twisted-names python-twisted-mail python-twisted-words python-twisted-web python-zope.interface python-openssl openjdk-6-jdk mercurial
cd /opt/
hg clone http://hg.qwebirc.org/qwebirc/
cp config.py.example config.py
vi config.py
# fix your config up!
python compile.py
# start it up on port 80 :)
python run.py -p 80
# visit your ip in your browser

and done, have fun :)

Categories: bash, server, xen Tags:

new service: Youtube Counter

August 13th, 2012 No comments

I wanted to integrate a simple Youtube views and comments counter into my website.

Turned out, there was no simple solution to this problem.

So i created a little service that enables you to do exactly this: http://ytcounter.com

Live Example:


Our video got .

The other videos got .

Categories: coding Tags:

building ZXing as JNI lib

March 6th, 2012 3 comments

i wanted to compare the speed of the cpp and java version of ZXing.

So after fixing some general linux related bugs, it seems to work well as start:

(the following only works with SVN rev. 2218, could be slightly different for later revisions)

0. install the Android NDK

1. check out zxing:

1
2
svn checkout http://zxing.googlecode.com/svn/trunk/ zxing-read-only
cd zxing-read-only

2. fix linux bugs:

1
2
wget http://thomasfischer.biz/wp-content/2012/03/linux-fixes.diff
patch -p0 < linux-fixes.patch

3. setup JNI:

1
2
3
mkdir jni
wget http://thomasfischer.biz/wp-content/2012/03/android-jni.diff
patch -p0 < android-jni.diff

4. build it:

1
2
3
cd android
export NDKHOME=/your/ndk/path/android-ndk-r7b/
ndk-build -j8

i contacted some author of the lib, maybe this will go upstream,
have fun :)

Categories: android, coding Tags:

vector drawing lib Anti-Grain combined with Ogre

February 4th, 2012 No comments

developed a little ogre application:
http://www.ogre3d.org/forums/viewtopic.php?f=11&t=68749

Categories: coding, Ogre3D Tags: