In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "with statement instance usage in python". In daily operation, I believe many people have doubts about the with statement instance usage in python. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "with statement instance usage in python"! Next, please follow the small series to learn together!
Today, my colleague came to me and said that he couldn't connect to the database with pyton. For security reasons, our database does not allow direct connections and requires connections through springboards. So, I think he didn't use the SSH tunnel. After looking at the code, I found that the sshtunnel module was used.
After taking his code, he ran it himself. 2013, 'Lost connection to MySQL server during query'. Thought it was the network, and then run it a few times, or report the same error. After checking, there was no problem with the connection information. Checking the network, there was no abnormality. He was dumbfounded and at a loss. I'm going to have to deal with other issues first. When I got home at night, I, who had OCD, reopened the code and looked at it carefully.
with SSHTunnelForwarder( (ip, port), #B Configuration of the machine ssh_username="user", ssh_password="password", remote_bind_address=('ip', port) #logger=create_logger(loglevel=1)) as server: conn = MySQLdb.connect(host='127.0.0.1', port=server.local_bind_port, username='username' password='password' db='db' charset='utf8' )cursor = conn.cursor()sql = 'select * from test limit 1'
The above code, at first did not find where wrong, but later saw that error, and thought of a few days ago to see the use of python with, seems to have a clue. Put sql code inside the with block and execute it. Everything was okay, and he suddenly felt enlightened. This is inseparable from the nature of the with statement. The with statement is suitable for accessing resources, ensuring that no matter whether an exception occurs during use, the necessary "cleaning" operation will be performed to release resources, such as automatic closing of files after use, automatic acquisition and release of locks in threads, etc.
At this point, the study of "with statement instance usage in python" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.