Learning Python Network Programming

(Sean Pound) #1
Chapter 7
# get remote cert
cert = ssl_conn.getpeercert()
print("Checking server certificate")
pprint(cert)
if not cert or ssl.match_hostname(cert, target_host):
raise Exception("Invalid SSL cert for host %s. Check if
this is a man-in-the-middle attack!" %target_host )
print("Server certificate OK.\n Sending some custom request...
GET ")
ssl_conn.write('GET / \n'.encode('utf-8'))
print("Response received from server:")
print(ssl_conn.read())
ssl_conn.close()

Running the client/server will show output similar to the following screenshot.
Can you see any difference in comparison to our last example client/server
communication?

Free download pdf