Guest User

Untitled

a guest
Dec 1st, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import psycopg2
  2. import time
  3.  
  4. conn = psycopg2.connect("host='dbs' dbname='foo' user='joe' password='x'")
  5. time.sleep(10) # I manually turn VPN off during this sleep..
  6. cu = conn.cursor()
  7. cu.execute('SELECT 1') # <- hangs here
  8. print cu.fetchone()
  9. cu.commit()
  10.  
  11. import socket
  12. socket.setdefaulttimeout(10)
  13.  
  14. ..
  15. conn = psycopg2.connect(...
  16. s = socket.fromfd(conn.fileno(), socket.AF_INET, socket.SOCK_STREAM)
  17. s.settimeout(5)
  18. ..
  19.  
  20. ...
  21. conn = psycopg2.connect(...
  22. s = socket.fromfd(conn.fileno(), socket.AF_INET, socket.SOCK_STREAM)
  23. s.settimeout(5)
  24. s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  25. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
  26. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
  27. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
  28. ...
  29.  
  30. s = socket.fromfd(connection.fileno(),
  31. socket.AF_INET, socket.SOCK_STREAM)
  32. # Enable sending of keep-alive messages
  33. s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
  34. # Time the connection needs to remain idle before start sending
  35. # keepalive probes
  36. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, int(ceil(time)))
  37. # Time between individual keepalive probes
  38. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 1)
  39. # The maximum number of keepalive probes should send before dropping
  40. # the connection
  41. s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
Add Comment
Please, Sign In to add comment