Networking
Collection of networking over sockets, using HTTP libraries, and networking
Sockets
Basic network communication over raw sockets
# Basic TCP connection to a server to send and receive data
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('127.0.0.1', 9987))
clientsocket.send('hello'.encode())
data = clientsocket.recv(1024)
print(data)SSH Interaction
Paramiko is a useful library which can be used to log into a host over SSH and execute commands as though it were an interactive session.
import paramiko
key = '/home/some/rsa/private/key'
hostname = '127.0.0.1'
user = 'm4lwhere'
k = paramiko.RSAKey.from_private_key_file(key)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=user, pkey=k)
stdin, stdout, stderr = client.exec_command('ls -l')
print(f'Received output:\n\n{stdout}')
# Don't forget to close out the object!
ssh.close()SFTP
Paramiko can additionally support SFTP natively with a paramiko.SSHClient() object which is super cool.
Last updated
Was this helpful?