Merge pull request #4084 from benleb/pythonic-fw-server

introduce common python coding styles & other codestyle fixes
This commit is contained in:
Theo Arends 2018-10-25 14:23:37 +02:00 committed by GitHub
commit d9b9eeef1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,22 +1,23 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# coding=utf-8
""" """
fw-server.py - firmware server for Sonoff-Tasmota OTA upgrade fw-server.py - firmware server for Sonoff-Tasmota OTA upgrade
Copyright (C) 2018 Gennaro Tortone Copyright (C) 2018 Gennaro Tortone
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Requirements: Requirements:
- Python3 - Python3
@ -31,26 +32,28 @@ Instructions:
Firmware Upgrade -> Upgrade by web server Firmware Upgrade -> Upgrade by web server
http://<ip_address>:5000/sonoff-minimal.bin http://<ip_address>:5000/sonoff-minimal.bin
Usage: Usage:
./fw-server.py -d <net_iface> (default: eth0) ./fw-server.py -d <net_iface> (default: eth0)
or or
./fw-server.py -i <ip_address> ./fw-server.py -i <ip_address>
Example: Example:
./fw-server.py -d wlan0 ./fw-server.py -d wlan0
or or
./fw-server.py -i 192.168.1.10 ./fw-server.py -i 192.168.1.10
""" """
import io
import os.path import os.path
from sys import exit
from flask import Flask, send_file
from optparse import OptionParser from optparse import OptionParser
from sys import exit
from flask import Flask, send_file
import netifaces as ni import netifaces as ni
usage = "usage: fw-server {-d | -i} arg" usage = "usage: fw-server {-d | -i} arg"
parser = OptionParser(usage) parser = OptionParser(usage)
parser.add_option("-d", "--dev", action="store", type="string", parser.add_option("-d", "--dev", action="store", type="string",
dest="netdev", default="eth0", help="network interface (default: eth0)") dest="netdev", default="eth0", help="network interface (default: eth0)")
@ -58,23 +61,30 @@ parser.add_option("-i", "--ip", action="store", type="string",
dest="ip", help="IP address to bind") dest="ip", help="IP address to bind")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
netip = None
if options.ip is None: if options.ip is None:
try: try:
netip = ni.ifaddresses(options.netdev)[ni.AF_INET][0]['addr'] netip = ni.ifaddresses(options.netdev)[ni.AF_INET][0]['addr']
except Exception as e: except Exception as e:
print("E: network interface error - {}".format(e)) print("E: network interface error - {}".format(e))
exit(1) exit(1)
else: else:
netip = options.ip netip = options.ip
app = Flask(__name__) app = Flask(__name__)
@app.route('/<filename>') @app.route('/<filename>')
def fw(filename): def fw(filename):
if(os.path.exists("fw/" + str(filename))): if os.path.exists("fw/" + str(filename)):
return send_file("fw/" + str(filename), attachment_filename=filename, mimetype='application/octet-stream') return send_file("fw/" + str(filename),
else: attachment_filename=filename,
return("ERROR: file not found") mimetype='application/octet-stream')
return "ERROR: file not found"
if __name__ == "__main__": if __name__ == "__main__":
try: try: