Skip to content

Instantly share code, notes, and snippets.

@richvdh
Created April 9, 2021 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richvdh/909761ff5dab23f0873eeddd7936a740 to your computer and use it in GitHub Desktop.
Save richvdh/909761ff5dab23f0873eeddd7936a740 to your computer and use it in GitHub Desktop.
twisted starttls test
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.
#!/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