Benutzer-Werkzeuge

Webseiten-Werkzeuge


internet-bootserver

Internet-Bootserver

boot.py
#!/usr/bin/python2.5
# Internet-Bootserver for Internet-Bootloader.
# See http://s.wangnick.de/doku.php?id=iot-basisstation for Internet-Bootloader on target hardware.
# (C) Copyright 2014 Sebastian Wangnick.
# Usage under "CC Attribution-Noncommercial-Share Alike 3.0 Unported" as described in http://creativecommons.org/licenses/by-nc-sa/3.0/ is granted.
 
# The following tables are required:
#
# CREATE TABLE `SWID` (
#   `swid` text NOT NULL,
#   `swfile` text NOT NULL,
#   `hwid` text,
#   `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
# ) DEFAULT CHARSET=utf8;
# 
# CREATE TABLE `BOOTLOG` (
#   `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
#   `hwid` text NOT NULL,
#   `loaderid` text,
#   `mcusr` varchar(2) DEFAULT NULL,
#   `swfile` text,
#   `swid` text
# ) DEFAULT CHARSET=utf8;
 
import sys
try:
    import cgi
    import shutil
 
    # Retrieve URI arguments into dict
    form = cgi.FieldStorage()
    params = dict([(key,form.getfirst(key)) for key in form.keys()])
 
    # Extract mandatory arguments
    hwid = params.pop('hwid',None)
    loaderid = params.pop('loaderid',None)
    mcusr = params.pop('mcusr',None)
    if not hwid or params:
        print 'Status: 400 Bad Request'
        print
        print 'Status: 400 Bad Request'
        sys.exit()
 
    # Connect to MySQL
    import MySQLdb
    db = MySQLdb.connect(host='mysql5.wangnick.com',user='db36289_3',passwd='<passwd>',db='db36289_3')
    cur = db.cursor()
 
    # check which software to load on this hardware
    sql = 'SELECT swfile,swid FROM SWID WHERE hwid = "%s" ORDER BY date DESC'%hwid
    cur.execute(sql)
    swfilerow = cur.fetchone()
 
    # Log boot request
    sql = 'INSERT INTO BOOTLOG (hwid,loaderid,mcusr,swfile,swid) VALUES (%s,%s,%s,%s,%s)'
    cur.execute(sql,(hwid,loaderid,mcusr,swfilerow[0],swfilerow[1]))
 
    # Serve the file
    print
    if swfilerow:
        swf = open(swfilerow[0])
        shutil.copyfileobj(swf,sys.stdout)
        swf.close()
except SystemExit:
    raise
except:
    print 'Status: 500 Internal Server Error'
    print
    print 'Status: 500 Internal Server Error'
    import traceback
    print '\n'.join(traceback.format_exception(*sys.exc_info()))
    raise
internet-bootserver.txt · Zuletzt geändert: 2015/04/06 14:35 von sebastian

Seiten-Werkzeuge