tnetconnect.nim 645 B

12345678910111213141516171819202122232425262728
  1. discard """
  2. matrix: "-d:ssl"
  3. """
  4. import std/net
  5. from std/strutils import `%`
  6. # bug #15215
  7. proc test() =
  8. let ctx = newContext()
  9. proc fn(url: string) =
  10. let socket = newSocket()
  11. defer: close(socket)
  12. connect(socket, url, Port(443), 5000) # typically 20 could be enough
  13. send(socket, "GET / HTTP/1.0\nHost: $#\nConnection: close\n\n" % [url])
  14. wrapSocket(ctx, socket)
  15. # trying 2 sites makes it more resilent: refs #17458 this could give:
  16. # * Call to 'connect' timed out. [TimeoutError]
  17. # * No route to host [OSError]
  18. try:
  19. fn("www.nim-lang.org")
  20. except TimeoutError, OSError:
  21. fn("www.google.com")
  22. test()