Python Schulungs Skript: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
Zeile 91: Zeile 91:
 
   i = i + 1
 
   i = i + 1
 
</syntaxhighlight>
 
</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=
 
=Links=
 
*https://de.wikipedia.org/wiki/Python_(Programmiersprache)
 
*https://de.wikipedia.org/wiki/Python_(Programmiersprache)

Version vom 28. November 2019, 16:03 Uhr

Grundlegendes

  • Entwickelt von Guido van Rossum am Centrum Wiskunde & Informatica in Amsterdam
  • Der Name bezieht sich auf die Monty Phytons
  • eine universelle, üblicherweise interpretierte höhere Programmiersprache.
  • Sie hat den Anspruch, einen gut lesbaren, knappen Programmierstil zu fördern.
  • Blöcke werden nicht durch geschweifte Klammern, sondern durch Einrückungen strukturiert.
  • klare und übersichtlichen Syntax
  • Objektorientiert, Aspektorientiert, Funktionale

Der Anfang "Hello World"

Der simpelste Befehl in Python ist die Ausgabe einer Zeichenfolge. Dazu gibt es den Befehl print()

#!/usr/bin/python
print("Hallo Welt!")

Das Skript sollte man natürlich noch ausführbar machen

  • chmod +x skript

Variablen

Skalare Variablen

#!/usr/bin/python
x = "Hallo Welt!"
print(x)

Arrays oder Feldvariablen

#!/usr/bin/python
mylist=['red', 'green', 'blue']
print(mylist[0])
print(mylist[1])
print(mylist[2])

Die Eingabe

Einfache Eingabe

Die Benutzereingabe wird durch den Befehl input() realistiert

#!/usr/bin/python
eingabe=input("Geben Sie etwas ein\n")
print(eingabe)

Datentyp der Eingabe ist ein String

Bei Eingaben von Zahlen werden 2 Strings aneinander gereiht

#!/usr/bin/python
a = input('Bitte geben Sie eine Zahl ein: ')
b = input('Bitte geben Sie eine zweite Zahl ein: ')
print(a + b)

Umwandlung von Strings in Integer

Hier werden die Zahlen addiert

#!/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)

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]

#!/usr/bin/python
import sys 
print(sys.argv[0]) 
print(sys.argv[1])
print(sys.argv[2])
print(len(sys.argv)

Die Verzweigung if

Bei if wird gecheckt ob eine Bedingung zutrifft. Wenn ja wird der erste Block durchgeführt. Ansonsten der zweite Block.

#!/usr/bin/python
import sys
x = sys.argv[1]
if x == "xinux":
  print("Good Company")
else:
  print("Ok")
print "End"

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.

#!/usr/bin/python
import sys
i = 1
x = sys.argv[1]
while i <= int(x):
  print(i)
  i = i + 1

Scripte

Netzwerkcheck

#!/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()

Links