Libraries Flashcards
Server/client socket
Socket:
CLIENT:
target_host = "www.google.com" target_port = 80
# create a socket object client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client client.connect((target_host,target_port))
# send some data client.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")
# receive some data response = client.recv(4096) print(response.decode()) client.close()
SERVER: import socket import threading IP = '0.0.0.0' PORT = 9998
def main()
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((IP, PORT))
server.listen(5)
print(f’[] Listening on {IP}:{PORT}’)
while True:
client, address = server.accept()
print(f’[] Accepted connection from {address[0]}:{address[1]}’)
client_handler = threading.Thread(target=handle_client,
args=(client,))
client_handler.start()
def handle_client(client_socket): with client_socket as sock: request = sock.recv(1024) print(f'[*] Received: {request.decode("utf-8")}') sock.send(b'ACK')
if __name__ == ‘__main__’:
main()
Threading
Run multiple tasks at the same time. Not wait until the end of execution.
import threading
import time
def loop1_10(): for i in range(1, 11): time.sleep(1) print(i)
threading.Thread(target=loop1_10).start()
Argparse
Parser library: parser = argparse.ArgumentParser() parser.add_argument('-c', '--command', action='store_true', help='command shell') args = parser.parse_args() print(args.command)
Shlex
Lexical analyszer, got .split .quotes() etc..
shlex. join()
shlex. split()
subprocess
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
subprocess.run([“ls”, “-l”])
sys
sys.argv
The list of command line arguments passed to a Python script
File objects used by the interpreter for standard input, output and errors
sys. stdin
sys. stdout
sys. stderr
EX:
sys.stdin.read()
paramiko
SSH client/server
mysql.connector
mydb = mysql.connector.connect( host="1.1.1.", user="user", passwd='password', database="database", ssl_disabled=True, autocommit=True ) base = mydb.cursor(prepared=True) sql = "SELECT from radcheck where username= %s" usertodel = (username, ) radius.execute(sql, usertodel) mydb.commit() mydb.close()
Request + JSON
url=”http://api.ipstack.com/”+targetIP+”?access_key=”+api
lookup = requests.get(url)
result=json.loads(lookup.content) country=result['country_name'] isp=result['connection']['isp']