Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import socket
  2. import ssl
  3. import tornado.gen
  4. import tornado.ioloop
  5. import tornado.iostream
  6. import tornado.tcpclient
  7. import tornado.test.iostream_test
  8.  
  9.  
  10. @tornado.gen.coroutine
  11. def case1(host, port):
  12. ssl_context = ssl.create_default_context()
  13. try:
  14. stream = yield tornado.tcpclient.TCPClient().connect(
  15. host=host, port=port, ssl_options=ssl_context)
  16. except ValueError as e:
  17. print(e) # check_hostname requires server_hostname
  18. raise
  19.  
  20.  
  21. @tornado.gen.coroutine
  22. def case2(stream_future):
  23. stream = yield stream_future
  24. try:
  25. sslstream = yield stream.start_tls(
  26. server_side=False,
  27. ssl_options=ssl.create_default_context(),
  28. server_hostname='example.com')
  29. except ValueError as e:
  30. print(e) # check_hostname requires server_hostname
  31. raise
  32.  
  33.  
  34. if __name__ == '__main__':
  35. ioloop = tornado.ioloop.IOLoop.current()
  36.  
  37. # Case 1
  38. #ioloop.add_callback(lambda: case1('example.com', 443))
  39.  
  40. # Case 2
  41. stream = tornado.iostream.IOStream(socket.socket())
  42. ioloop.add_future(stream.connect(('example.com', 443)), case2)
  43.  
  44. ioloop.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement