Created
April 9, 2021 22:47
-
-
Save richvdh/909761ff5dab23f0873eeddd7936a740 to your computer and use it in GitHub Desktop.
twisted starttls test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Started to connect. | |
Connected. | |
Protocol lost connection. Reason: VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b'not-google.com'))]) | |
Factory lost connection. Reason: Connection was aborted locally using ITCPTransport.abortConnection. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from twisted.internet import reactor | |
from twisted.internet.protocol import ClientFactory, Protocol | |
from twisted.internet.ssl import optionsForClientTLS | |
class StartTLSClient(Protocol): | |
def connectionMade(self): | |
print("Connected.") | |
context = optionsForClientTLS("not-google.com") | |
self.transport.startTLS(context) | |
def connectionLost(self, reason): | |
print("Protocol lost connection. Reason: %s" % reason.value) | |
class StartTLSClientFactory(ClientFactory): | |
def startedConnecting(self, connector): | |
print("Started to connect.") | |
def buildProtocol(self, addr): | |
return StartTLSClient() | |
def clientConnectionLost(self, connector, reason): | |
print("Factory lost connection. Reason: %s" % reason.value) | |
reactor.connectTCP("www.google.com", 443, StartTLSClientFactory()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment