Latest articles:

CCCac Clubstatus

aus der Kategorie Open Source

Das ist der aktuelle Code um den Clubstatus abzufragen:
#!/usr/bin/env python
# -*- coding: latin-1 -*-
## VERSION: Freitag 23 Dezember 2011, 23:27

import base64
import socket
import ssl
import sys

import gtk
import pygtk

pygtk.require("2.0")
import gobject


class status_checker:
    def __init__(self):
        self.statusIcon = gtk.StatusIcon()
        self.statusIcon.set_from_file("undefined.ico")
        self.status = -1
        self.statusIcon.set_visible(True)

        self.HOST = "status.voidspace.aachen.ccc.de"

        # temp
        USER = "user"
        # passwort="password"
        self.ARG = base64.b64encode(USER + ":" + passwort)
        self.check_status()

        self.statusIcon.connect("popup-menu", self.callback)

    def __del__(self):
        pass

    def exit(self, widget, data=None):
        gtk.main_quit(0)

    def callback(self, icon, button, time):
        menu = gtk.Menu()
        menuitem = gtk.MenuItem("Toggle Status")
        menuitem.connect("activate", self.toggle_status)
        menu.append(menuitem)
        menuitem = gtk.MenuItem("Refresh")
        menuitem.connect("activate", self.check_status)
        menu.append(menuitem)
        menu.append(gtk.SeparatorMenuItem())
        menuexit = gtk.MenuItem("Exit")
        menuexit.connect("activate", self.exit)
        menu.append(menuexit)
        menu.show_all()
        menu.popup(
            None, None, gtk.status_icon_position_menu, button, time, self.statusIcon
        )

    def check_announce(self, widget=None):
        pass

    def change_status(self, status):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            ssl_sock = ssl.wrap_socket(
                s, ca_certs="/etc/ca_certs_file", cert_reqs=ssl.CERT_NONE
            )  ##we don't care about certs
            ssl_sock.connect((self.HOST, 443))
            if status:
                MESSAGE = (
                    """POST /status HTTP/1.1\r\nHost: """
                    + self.HOST
                    + """\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 4\r\nAuthorization: Basic """
                    + self.ARG
                    + """\r\n\r\nopen\r\n\r\n"""
                )
            else:
                MESSAGE = (
                    """POST /status HTTP/1.1\r\nHost: """
                    + self.HOST
                    + """\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 6\r\nAuthorization: Basic """
                    + self.ARG
                    + """\r\n\r\nclosed\r\n\r\n"""
                )
            ssl_sock.write(MESSAGE)
            data = ssl_sock.read()
            if data.find("Status: 201"):
                self.status = status
                if status:
                    self.statusIcon.set_from_file("opened.ico")
                    print "Status has been changed to open"
                else:
                    self.statusIcon.set_from_file("closed.ico")
                    print "Status has been changed to closed"
            else:
                print "Status has not been changed"
            s.close()
        except:
            print "There hast been a problem"

    def toggle_status(self, widget=None):
        self.change_status(not self.status)

    def check_status(self, widget=None, data=None):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            ssl_sock = ssl.wrap_socket(
                s, ca_certs="/etc/ca_certs_file", cert_reqs=ssl.CERT_NONE
            )  ##we don't care about certs
            ssl_sock.connect((self.HOST, 443))
            ssl_sock.write(
                """GET /status HTTP/1.1\r\nHost: """
                + self.HOST
                + """\r\nAuthorization: Basic """
                + self.ARG
                + """\r\n\r\n"""
            )
            data = ssl_sock.read()
            print data
            if not data.find("Status: 200") == -1:
                data = ssl_sock.read()
                print data
                if not data.find("closed") == -1:
                    self.status = 0
                    self.statusIcon.set_from_file("closed.ico")
                else:
                    self.status = 1
                    self.statusIcon.set_from_file("opened.ico")
            else:
                self.statusIcon.set_from_file("undefined.ico")
                self.status = -1
            s.close()
            return True
        except:
            self.statusIcon.set_from_file("undefined.ico")
            self.status = -1
            print "Warning connection error!"
            return True

    def run(self):
        self.timer = gobject.timeout_add(1000 * 10 * 60, self.check_status)
        gtk.main()


def main(argv=None):
    A = status_checker()
    A.run()
    sys.exit(0)  ##everything done


if __name__ == "__main__":
    main(sys.argv)

hzgf. am 23. Dezember 2011