Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2017 13:56
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 anonymous/e46d4d8dc1c102857497cde9e2fc4725 to your computer and use it in GitHub Desktop.
Save anonymous/e46d4d8dc1c102857497cde9e2fc4725 to your computer and use it in GitHub Desktop.
Twisted-HTTPS-client-server
getting this Error:
Connected to pydev debugger (build 172.3757.67)
Error during info_callback
Traceback (most recent call last):
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/twisted/protocols/tls.py", line 315, in dataReceived
self._checkHandshakeStatus()
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/twisted/protocols/tls.py", line 235, in _checkHandshakeStatus
self._tlsConnection.do_handshake()
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1715, in do_handshake
result = _lib.SSL_do_handshake(self._ssl)
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/OpenSSL/SSL.py", line 1136, in wrapper
callback(Connection._reverse_mapping[ssl], where, return_code)
--- <exception caught here> ---
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/twisted/internet/_sslverify.py", line 1102, in infoCallback
return wrapped(connection, where, ret)
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/twisted/internet/_sslverify.py", line 1201, in _identityVerifyingInfoCallback
verifyHostname(connection, self._hostnameASCII)
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/service_identity/pyopenssl.py", line 48, in verify_hostname
obligatory_ids=[DNS_ID(hostname)],
File "/home/ohadsh/workspace/apd_linux_DMS_Connectivity/venv/lib/python3.5/site-packages/service_identity/_common.py", line 245, in __init__
raise ValueError("Invalid DNS-ID.")
builtins.ValueError: Invalid DNS-ID.
HTTPS Twisted Server Code :
from twisted.internet import reactor, ssl
from twisted.web.server import Site
from twisted.web.resource import Resource
class DMSPage(Resource):
isLeaf = True
def render_GET(self, request):
return "<html><body>%s</body></html>" % (time.ctime(),)
def render_POST(self, request):
print('GOT to render_POST')
if __name__ == "__main__":
resource = DMSPage()
factory = Site(resource)
######## SSL certs #################
certAuthCert = getModule(__name__).filePath.sibling(
'hostkey.pem').getContent()
serverCert = getModule(__name__).filePath.sibling(
'hostcert.pem').getContent()
serverCert = ssl.PrivateCertificate.loadPEM(
certAuthCert + serverCert)
ctx = serverCert.options()
#######################################
reactor.listenSSL(6002, factory, ctx)
reactor.run()
HTTPS Twisted CLIENT Code:
from twisted.web.client import Agent, BrowserLikePolicyForHTTPS
from twisted.internet import reactor, ssl
HOST = '127.0.0.1'
PORT = '6002'
def cbResponse(*args, **kwargs):
print (args, kwargs)
print ('Response received')
def cbShutdown(ignored):
reactor.stop()
if __name__ == "__main__":
pem_path = os.path.join(os.path.dirname(
os.path.abspath(__file__)), "hostcertkey.pem")
agent = Agent(reactor, BrowserLikePolicyForHTTPS(
MyCATrustRoot(root_certificate_path=pem_path)))
url = "https://%s:%s" % (HOST, PORT)
url_in_bytes = url.encode('utf-8')
d = agent.request(b'POST', url_in_bytes)
d.addCallbacks(cbResponse, cbError)
d.addBoth(cbShutdown)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment