Python - Basics

strings

txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
txt2 = "My name is {0}, I'm {1}".format("John",36)
txt3 = "My name is {}, I'm {}".format("John",36) 

Datentypen

dicts

https://realpython.com/python-dicts/ ## Print ### formatierte Ausgabe https://www.python-kurs.eu/python3_formatierte_ausgabe.php

print("Zweites Argument: {1}, erstes: {0}".format(47,11))
print("Zweites Argument: {1:3d}, erstes: {0:7.2f}".format(47.42,11))
print("Erstes Argument: {}, zweites: {}".format(47,11))
print("precisions: {0:6.2f} or {0:6.3f}".format(1.4148)) 
for i in range(5):
   print(i)

File read/write

read

#!/usr/bin/env python3
import requests
 
# URL
URL="http://10.10.10.10:8080/manager/html"
# USERNAME and PASSWORDS
PWDFILE="/usr/share/seclists/Passwords/Default-Credentials/tomcat-betterdefaultpasslist.txt"
 
with open(PWDFILE, 'r') as f:
        for line in f:
            line = line.strip().split(":")
            # print(line)
 
            # get site with auth
            r = requests.get(URL, auth=(line[0],line[1]))
 
            # Statuscode:403 fails, 200 OK
            #print(r.status_code)
            if r.status_code == 200:
                print(line)

Command Line opts

Args und kwargs

Vererbung und super

Debugger

#pudbg aktivieren:
export PYTHONBREAKPOINT="pudb.set_trace"

Debuger ausschalten: https://www.python.org/dev/peps/pep-0553/

PYTHONBREAKPOINT=0 #disables debugging

https://realpython.com/python-debugging-pdb/
https://docs.python.org/3.8/library/pdb.html

pudb

using:

<!-- -->
#this in ~/.bash_rc
export PYTHONBREAKPOINT="pudb.set_trace"

# then:
# breakpoint() in Sourcecode

    c Continue the execution of the program,
    s Step into the function,
    r Go to the return line of current function,
    n Go to the next line,
    u Go one level up the stack,
    d Go one level down the stack,
    b Set a breakpoint or remove a breakpoint,
    CTRL + V Focus on variables tab,
    CTRL + C Focus on code tab,
    CTRL + X Focus on shell tab or back to code when on shell tab.

Sockets

TCP Client

#!/usr/bin/env python3
import socket
 
target_host = "www.google.com"
target_host_port = 80
 
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_host_port))
 
client.send(b'GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n')
answ = client.recv(4096)
print(answ)

TCP Server

#!/usr/bin/env python3
 
import socket
import threading
 
bindip = "0.0.0.0"
bindport = 9876
 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bindip, bindport))
server.listen(3)
print("Server hört auf: IP {}, Port {}".format(bindip, bindport))
 
 
def client_handler(client_sock):
    req = client_sock.recv(1024)
    print("nachricht: {}".format(req))
    client_sock.send(b'CHECK')
    client_sock.close()
 
while True:
    client,addr = server.accept()
    print("Verbindung von {} : {}".format(addr[0],addr[1]))
    client_handel = threading.Thread(target=client_handler, args=(client,))
    client_handel.start()

Multithreading

URL libs

PEP 8

Web scrapping

request

cookies = {}
cookies ["sessionid"] = "1234abc"
cookies ["garbage"] = "14bc"
r = request.post("url-addrr",cookies=cookies)

BS4

soup = BeautifulSoup(r.content, 'html.parser')
result = soup.find(id="Product")
# result = soup.find(id="Product").find("img")[src]
print(result)
print(result.text)