|
|
| (11 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) |
| Zeile 1: |
Zeile 1: |
| =Grundlegendes=
| | *[[Python Grundlegendes]] |
| *Entwickelt von Guido van Rossum am Centrum Wiskunde & Informatica in Amsterdam | | *[[Python Hello World]] |
| *Der Name bezieht sich auf die Monty Phytons
| | *[[Python Variablen]] |
| *eine universelle, üblicherweise interpretierte höhere Programmiersprache.
| | *[[Python Eingabe]] |
| *Sie hat den Anspruch, einen gut lesbaren, knappen Programmierstil zu fördern.
| | *[[Python Argumentenübergabe]] |
| *Blöcke werden nicht durch geschweifte Klammern, sondern durch Einrückungen strukturiert.
| | *[[Python if]] |
| *klare und übersichtlichen Syntax
| | *[[Python case]] |
| *Objektorientiert, Aspektorientiert, Funktionale
| | *[[Python while]] |
| =Der Anfang "Hello World"=
| | *[[Python for]] |
| Der simpelste Befehl in Python ist die Ausgabe einer Zeichenfolge. Dazu gibt es den Befehl '''print()'''
| | *[[Python Dateihandling]] |
| <syntaxhighlight>
| | *[[Python Funktionen]] |
| #!/usr/bin/python
| | *[[Python Klassen]] |
| print("Hallo Welt!")
| | *[[Python Skripte]] |
| </syntaxhighlight>
| | =Quellen= |
| Das Skript sollte man natürlich noch ausführbar machen
| | *https://www.python-kurs.eu/python3_memoisation.php |
| *chmod +x skript | |
| =Variablen=
| |
| ==Skalare Variablen==
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| x = "Hallo Welt!"
| |
| print(x)
| |
| </syntaxhighlight>
| |
| ==Arrays oder Feldvariablen==
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| mylist=['red', 'green', 'blue']
| |
| print(mylist[0])
| |
| print(mylist[1])
| |
| print(mylist[2])
| |
| </syntaxhighlight>
| |
| =Die Eingabe=
| |
| ==Einfache Eingabe==
| |
| Die Benutzereingabe wird durch den Befehl '''input()''' realistiert
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| eingabe=input("Geben Sie etwas ein\n")
| |
| print(eingabe)
| |
| </syntaxhighlight>
| |
| ==Datentyp der Eingabe ist ein String==
| |
| Bei Eingaben von Zahlen werden 2 Strings aneinander gereiht
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| a = input('Bitte geben Sie eine Zahl ein: ')
| |
| b = input('Bitte geben Sie eine zweite Zahl ein: ')
| |
| print(a + b)
| |
| </syntaxhighlight>
| |
| ==Umwandlung von Strings in Integer==
| |
| Hier werden die Zahlen addiert
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| a = eval(input('Bitte geben Sie eine Zahl ein: '))
| |
| b = eval(input('Bitte geben Sie eine zweite Zahl ein: '))
| |
| print(a + b)
| |
| </syntaxhighlight>
| |
| =Argumenteübergabe beim Aufruf=
| |
| Wir müssen hier das Modul sys einbinden
| |
| | |
| Der Skriptname steht in der Variable sys.argv[0], die Parameterin in sys.argv[n]
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| import sys
| |
| print(sys.argv[0])
| |
| print(sys.argv[1])
| |
| print(sys.argv[2])
| |
| print(len(sys.argv)
| |
| </syntaxhighlight>
| |
| =Die Verzweigung '''if'''=
| |
| Bei '''if''' wird gecheckt ob eine Bedingung zutrifft. Wenn ja wird der erste '''Block''' durchgeführt. Ansonsten der zweite Block.
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| import sys
| |
| x = sys.argv[1]
| |
| if x == "xinux":
| |
| print("Good Company")
| |
| else:
| |
| print("Ok")
| |
| print "End"
| |
| </syntaxhighlight>
| |
| =Die '''case''' Auswahl=
| |
| Es gibt keine native '''case''' Auswahl in Python :-)
| |
| =Die '''while''' Schleife=
| |
| Die '''while''' Schleife wird solange durchlaufen solange die Bedingung erfüllt ist.
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| import sys
| |
| i = 1
| |
| x = sys.argv[1]
| |
| while i <= int(x):
| |
| print(i)
| |
| i = i + 1
| |
| </syntaxhighlight>
| |
| =Scripte=
| |
| ==Netzwerkcheck==
| |
| <syntaxhighlight>
| |
| #!/usr/bin/python
| |
| #pip install pyping colorama wget
| |
| dat_sys="/usr/local/etc/funktion.cfg"
| |
| import os
| |
| import socket, datetime
| |
| import pyping,sys
| |
| from colorama import init, Fore, Style
| |
| dat=open(dat_sys,"r")
| |
| init()
| |
| def ping_status(name, hostname):
| |
| r = pyping.ping(hostname, timeout=100, count=1)
| |
| if r.ret_code == 0:
| |
| print(Style.RESET_ALL + " ping status " + name + " " + Fore.GREEN + "ok")
| |
| else:
| |
| print(Style.RESET_ALL + " ping status " + name + " " + Fore.RED + "failed")
| |
| | |
| def port_status(name,hostname,port):
| |
| socket.setdefaulttimeout(1)
| |
| start_time = datetime.datetime.now()
| |
| try:
| |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
| |
| connect = sock.connect_ex((hostname, int(port)))
| |
| if connect == 0:
| |
| print(Style.RESET_ALL + " port status " + name + " " + Fore.GREEN + "ok")
| |
| else:
| |
| print(Style.RESET_ALL + " port status hostname can't connect " + name + " " + Fore.RED + "not ok")
| |
| sock.close()
| |
| except socket.gaierror:
| |
| print(Style.RESET_ALL + " port status hostname can't resolved " + name + " " + Fore.RED + "not ok")
| |
| except socket.error:
| |
| print(Style.RESET_ALL + " port status hostname can't connect " + name + " " + Fore.RED + "not ok")
| |
| end_time = datetime.datetime.now()
| |
| | |
| | |
| def proxy_status(name,url,proxy,port):
| |
| x = os.system("env http_proxy="+proxy+":"+port + " " + " https_proxy="+proxy+":"+port + " " + "wget --spider --tries 3 -T 1 -q " + url)
| |
| if x == 0:
| |
| print(Style.RESET_ALL + " proxy status " + name + " " + Fore.GREEN + "ok")
| |
| else:
| |
| print(Style.RESET_ALL + " proxy status " + name + " " + Fore.RED + "not ok")
| |
| | |
| for l in dat:
| |
| line = l.rstrip().split(";")
| |
| if (len(line)) == 2:
| |
| ping_status(line[0], line[1])
| |
| if (len(line)) == 3:
| |
| port_status(line[0], line[1], line[2])
| |
| if (len(line)) == 4:
| |
| proxy_status(line[0], line[1], line[2], line[3])
| |
| | |
| dat.close()
| |
| </syntaxhighlight>
| |
| | |
| =Links= | |
| *https://de.wikipedia.org/wiki/Python_(Programmiersprache) | |