Skip to content
Snippets Groups Projects
Commit 050c90c3 authored by Ion-Dorinel FILIP (25005)'s avatar Ion-Dorinel FILIP (25005)
Browse files

Added lab12 code

parent e3ee79ab
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
import socket
import ssl
html = """HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE html><html><body><h1>Hello, world!</h1></body></html>"""
SERVER_CERT = './server.crt'
SERVER_PRIVATE = './server.key'
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
#context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) # Ubuntu 16.04
context.load_cert_chain(SERVER_CERT, SERVER_PRIVATE)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind(('0.0.0.0', 4433))
sock.listen(5)
while True:
newsock, fromaddr = sock.accept()
ssock = context.wrap_socket(newsock, server_side=True)
data = ssock.recv(1024) # Read data over TLS
ssock.sendall(html.encode('utf-8')) # Send data over TLS
ssock.shutdown(socket.SHUT_RDWR) # Close the TLS connection
ssock.close()
#!/usr/bin/python3
import socket, ssl, sys, pprint
# Primim ca argument hostname-ul serverului, de exemplu google.com
hostname = sys.argv[1]
port = 443
# Create TCP connection
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((hostname, port))
input("After making TCP connection. Press any key to continue ...")
# You may need to change this depending on your Linux distro
cadir = '/etc/ssl/certs'
# Set up the TLS context
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
#context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) # For Ubuntu 16.04
context.load_verify_locations(capath=cadir)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
# Add the TLS
ssock = context.wrap_socket(sock, server_hostname=hostname,
do_handshake_on_connect=False)
ssock.do_handshake() # Start the handshake
pprint.pprint(ssock.getpeercert())
input("After handshake. Press any key to continue ...")
# Close the TLS Connection
ssock.shutdown(socket.SHUT_RDWR)
ssock.close()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment