Network
socket
1 Π£Π·Π½Π°ΡΡ ΡΠ²ΠΎΠΉ IP
import socket
ip_v4 = socket.gethostbyname(socket.gethostname())2 Π£Π·Π½Π°ΡΡ, ΠΎΡΠΊΡΡΡ Π»ΠΈ ΠΏΠΎΡΡ:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
res = sock.connect_ex(('localhost', 12345))
if res == 0:
print("port is open")3 Raw http request
import socket
TARGET_HOST = 'www.example.com'
TARGET_PORT = 80
CONNECTION_TIMEOUT = 10
CHUNK_SIZE = 1024
HTTP_VERSION = 1.1
CRLF = '\r\n'
socket.setdefaulttimeout(CONNECTION_TIMEOUT)
def receive_all(sock, chunk_size=CHUNK_SIZE):
"""
Gather all the data from a request.
"""
chunks = []
while True:
try:
chunk = sock.recv(int(chunk_size))
if chunk:
chunks.append(chunk)
else:
break
except socket.timeout:
break
return b''.join(chunks)
# create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
res = client.connect_ex((TARGET_HOST, TARGET_PORT))
client.settimeout(0.5)
if res == 0:
print("port is open")
# send some data
request = f'GET / HTTP/{HTTP_VERSION}{CRLF}' \
+ f'Host: {TARGET_HOST}{CRLF}' \
+ f'{CRLF}'
client.sendall(request.encode())
print(f'Send request: \n{request}')
response = receive_all(client, CHUNK_SIZE)
client.shutdown(socket.SHUT_RDWR)
client.close()
print(f'Response: {response}')4 Raw http request (TLS)
aiortc
dnspython
paramiko [SSH]
scp
paramiko
sshtunnel
service_identity
twisted
Last updated