Created
April 22, 2010 19:39
-
-
Save martinklepsch/375707 to your computer and use it in GitHub Desktop.
Simple script to reconnect your fritz.box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Instruct an AVM FRITZ!Box via UPnP_ to reconnect. | |
This is usually realized with tools like Netcat_ or cURL_. However, when | |
developing in Python_ anyway, it is more convenient to integrate a native | |
implementation. This one requires Python_ 2.5 or higher. | |
UPnP_ (Universal Plug and Play) control messages are based on SOAP_, which is | |
itself based on XML_, and transmitted over HTTP_. | |
Make sure UPnP_ is enabled on the FRITZ!Box. | |
A reconnect only takes a few second while restarting the box takes about up to | |
a minute; not counting the time needed to navigate through the web interface. | |
.. _Netcat: http://netcat.sourceforge.net/ | |
.. _cURL: http://curl.haxx.se/ | |
.. _Python: http://www.python.org/ | |
.. _UPnP: http://www.upnp.org/ | |
.. _SOAP: http://www.w3.org/TR/soap/ | |
.. _XML: http://www.w3.org/XML/ | |
.. _HTTP: http://tools.ietf.org/html/rfc2616 | |
:Copyright: 2008 Jochen Kupperschmidt | |
:Date: 04-Apr-2008 | |
:License: MIT | |
:Homepage: http://homework.nwsnet.de/products/ef29_instruct-an-avm-fritzbox-via-upnp-to-reconnect | |
""" | |
from __future__ import with_statement | |
from contextlib import closing | |
import socket | |
def reconnect(host='fritz.box', port=49000, debug=False): | |
# Prepare HTTP data to send. | |
http_body = '\r\n'.join(( | |
'<?xml version="1.0" encoding="utf-8"?>', | |
'<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">', | |
' <s:Body>', | |
' <u:ForceTermination xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"/>', | |
' </s:Body>', | |
'</s:Envelope>')) | |
http_data = '\r\n'.join(( | |
'POST /upnp/control/WANIPConn1 HTTP/1.1', | |
'Host: %s:%d' % (host, port), | |
'SoapAction: urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination', | |
'Content-Type: text/xml; charset="utf-8"', | |
'Content-Length: %d' % len(http_body), | |
'', | |
http_body)) | |
# Connect to the box and submit SOAP data via HTTP. | |
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: | |
s.connect((host, port)) | |
s.send(http_data) | |
if debug: | |
data = s.recv(1024) | |
print 'Received:', data | |
if __name__ == '__main__': | |
reconnect() |
Cool, dass das Script sich vielen als nützlich erwiesen hat.
Ich habe ihm ein eigenes Repository bei GitHub spendiert und ein paar kleine Anpassungen für aktuelle Python-Versionen vorgenommen, vielleicht hilft es ja jemandem: https://github.com/homeworkprod/fritzbox-reconnect
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In der XML ist nur die Übersicht aller Services aufgeführt, die unterstützen
actions
werden in einer jeweils zusätzlichen XML aufgeführt.unter http://fritz.box:49000/tr64desc.xml
Findet man zum Beispiel
Folgt man nun http://fritz.box:49000/wanipconnSCPD.xml , dann erhält man die Auflistung aller actions des jeweiligen service