Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import dropbox, pymysql, shlex, shutil, subprocess
  2. from sshtunnel import SSHTunnelForwarder
  3. import iot_config as cfg
  4.  
  5. def CloseLocalDB():
  6. localcur.close()
  7. localdb.close()
  8.  
  9. def CloseRemoteDB():
  10. # Disconnect from the database
  11. # remotecur.close()
  12. # remotedb.close()
  13. # Close the SSH tunnel
  14. # ssh.close()
  15. print("end of CloseRemoteDB function")
  16.  
  17. def OpenLocalDB():
  18. global localcur, localdb
  19. localdb = pymysql.connect(host=cfg.localdbconn['host'], user=cfg.localdbconn['user'], passwd=cfg.localdbconn['passwd'], db=cfg.localdbconn['db'])
  20. localcur = localdb.cursor()
  21.  
  22. def OpenRemoteDB():
  23. global remotecur, remotedb
  24. with SSHTunnelForwarder(
  25. ('my_remote_site', 22),
  26. ssh_username = "my_ssh_username",
  27. ssh_private_key = "/etc/ssh/my_private_key.ppk",
  28. ssh_private_key_password = "my_private_key_password",
  29. remote_bind_address = ('127.0.0.1', 3308)) as server:
  30. remotedb = None
  31. #Following line gives an error if uncommented
  32. # remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)
  33. #remotecur = remotedb.cursor()
  34.  
  35. # Main program starts here
  36. OpenLocalDB()
  37. CloseLocalDB()
  38. OpenRemoteDB()
  39. CloseRemoteDB()
  40.  
  41. 2016-04-21 19:13:33,487 | ERROR | Secsh channel 0 open FAILED: Connection refused: Connect failed
  42. 2016-04-21 19:13:33,553 | ERROR | In #1 <-- ('127.0.0.1', 60591) to ('127.0.0.1', 3308) failed: ChannelException(2, 'Connect failed')
  43. ----------------------------------------
  44. Exception happened during processing of request from ('127.0.0.1', 60591)
  45. Traceback (most recent call last):
  46. File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 286, in handle
  47. src_address)
  48. File "/usr/local/lib/python3.4/dist-packages/paramiko/transport.py", line 834, in open_channel
  49. raise e
  50. paramiko.ssh_exception.ChannelException: (2, 'Connect failed')
  51.  
  52. During handling of the above exception, another exception occurred:
  53.  
  54. Traceback (most recent call last):
  55. File "/usr/lib/python3.4/socketserver.py", line 613, in process_request_thread
  56. self.finish_request(request, client_address)
  57. File "/usr/lib/python3.4/socketserver.py", line 344, in finish_request
  58. self.RequestHandlerClass(request, client_address, self)
  59. File "/usr/lib/python3.4/socketserver.py", line 669, in __init__
  60. self.handle()
  61. File "/usr/local/lib/python3.4/dist-packages/sshtunnel.py", line 296, in handle
  62. raise HandlerSSHTunnelForwarderError(msg)
  63. sshtunnel.HandlerSSHTunnelForwarderError: In #1 <-- ('127.0.0.1', 60591) to ('127.0.0.1', 3308) failed: ChannelException(2, 'Connect failed')
  64. ----------------------------------------
  65. Traceback (most recent call last):
  66. File "/home/pi/Documents/iot_pm2/iot_ssh_example_for_help.py", line 38, in <module>
  67. OpenRemoteDB()
  68. File "/home/pi/Documents/iot_pm2/iot_ssh_example_for_help.py", line 32, in OpenRemoteDB
  69. remotedb = pymysql.connect(host='127.0.0.1', user='remote_db_user', passwd='remote_db_password', db='remote_db_name', port=server.local_bind_port)
  70. File "/usr/local/lib/python3.4/dist-packages/pymysql/__init__.py", line 88, in Connect
  71. return Connection(*args, **kwargs)
  72. File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 678, in __init__
  73. self.connect()
  74. File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 889, in connect
  75. self._get_server_information()
  76. File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 1190, in _get_server_information
  77. packet = self._read_packet()
  78. File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 945, in _read_packet
  79. packet_header = self._read_bytes(4)
  80. File "/usr/local/lib/python3.4/dist-packages/pymysql/connections.py", line 981, in _read_bytes
  81. 2013, "Lost connection to MySQL server during query")
  82. pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement