Libraries Flashcards

1
Q

Server/client socket

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Threading

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Argparse

A
Parser library:
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--command', action='store_true', help='command shell')
args = parser.parse_args()
print(args.command)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Shlex

A

Lexical analyszer, got .split .quotes() etc..

shlex. join()
shlex. split()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

subprocess

A

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”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

sys

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

paramiko

A

SSH client/server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

mysql.connector

A
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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Request + JSON

A

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']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly