Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used operation and maintenance scripts in python?

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The purpose of this article is to share with you an introduction to the common operation and maintenance scripts used in python. The editor thinks it is very practical, so I will share it with you. Without saying much, let's read on.

1. Get the public network ip

#! / usr/bin/env python-*- coding:utf-8-*-Time: 2019-12-20 10:05import socketimport requests,re# method 1 text=requests.get ("http://txt.go.sohu.com/ip/soip").textip=re.findall(r'\d+.\d+.\d+.\d+',text)# method 2 ipqwb = socket.getaddrinfo ('www.baidu.com')" 'http') # get the A record nowIp of the specified domain name nowIp = (ipqwb [0] [4] [0]) # assign print ("local public network IP:" + ip [0]) print ("qwb IP:" + nowIp)

two。 Generate a random password:

#! / usr/bin/env python#-*-coding:utf-8-*-# Time: 2019-11-21 11:43import random,stringdef passwd (): src = string.ascii_letters + string.digits count = input ('Please confirm how many passwords to generate:') list_passwds = [] for i in range (int (count)): # the number of passwords is Nation3 For example, here is the 5'3'8-digit password list_passwd_all = random.sample (src, 5) # randomly take 5 digits of list_passwd_all.extend (random.sample (string.digits, 1)) # so that the password must contain the number list_passwd_all.extend (random.sample (string.ascii_lowercase) 1)) # make sure the password contains the lowercase letter list_passwd_all.extend (random.sample (string.ascii_uppercase) 1)) # make sure the password contains uppercase letters random.shuffle (list_passwd_all) # upset the order of the list str_passwd = '.join (list_passwd_all) # convert the list into a string if str_passwd not in list_passwds: # determine whether to generate a duplicate password list_passwds.append (str_passwd) print (list_passwds [I]) # print (list_passwds) passwd ()

3. To send an email:

#! / usr/bin/env python#-*-coding:utf-8-*-# Time: 2019-11-15 17:18import smtplibfrom email.mime.text import MIMETextfrom time import sleepfrom email.header import Headerhost = 'smtp.163.com'port = 25sender =' xxxx@163.com'pwd = 'xxxxx'receiver = [' 2222222222q.com, 'xxxxxxxx@163.com'] # you don't have to add your own mailbox Add to prevent error body = 'message content' title = 'message title' def sentemail (): msg = MIMEText (body, 'plain',' utf-8') msg ['subject'] = Header (title,' utf-8'). Encode () msg ['from'] = sender msg [' to'] =', '.join (receiver) try: s = smtplib.SMTP (host) Port) s.login (sender, pwd) s.sendmail (sender, receiver, msg.as_string ()) print ('Done.sent email success') except smtplib.SMTPException as e: print (' Error.sent email fail') print (e) if _ _ name__ = ='_ main__': sentemail ()

4. Basic log log configuration:

#! / usr/bin/env python#-*-coding:utf-8-*-# Time: 2019-11-27 13:04import loggingdef logger (): logger=logging.getLogger () fh=logging.FileHandler ("test.log") # sends content to the file with its own default log format ch=logging.StreamHandler () # sends the file to the screen Have your own default log format fm=logging.Formatter ("% (asctime) s% (message) s") # define your own log format fh.setFormatter (fm) # add a custom log format If not added, fh will be displayed with its default log format ch.setFormatter (fm) logger.addHandler (fh) # Ch log logger.addHandler (ch) logger.setLevel ("DEBUG") # define log level return logger # return function object logger=logger () # call function logger.debug ("hello 1") # print log logger.info ("hello 2") logger.warning ("hello 3") logger.error ("hello 4") logger.critical ("hello 5")

5. Check to see if the local port is open:

#! / usr/bin/env python#-*-coding:utf-8-*-# Time: 2019-11-21 11:05import socketport_number = [135 socket.socket 443 coding:utf-8 80 3306 22] for index in port_number: sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) result = sock.connect_ex (('127.0.0.1') Index)) if result = = 0: print ("Port% d is open"% index) else: print ("Port% d is not open"% index) sock.close ()

6. The decorator calls the function five times at a time

#! / usr/bin/env python-*- coding:utf-8-*-def again_func (func): def inner (* args, * kwargs): for line in range (5): func (* args, * * kwargs) return inner@again_funcdef func1 (): print ("from func1...") func1 ()

7. The difference between variable parameter definition * args, * * kwargs

#! / usr/bin/env python-*- coding:utf-8-*-def foo (* args, * * kwargs): print ("args=:", args) print ("kwargs=:", kwargs) print ("-") if _ _ name__ = ='_ main__': foo (1, 2) foo (1)

The above is the introduction of common operation and maintenance scripts in python. Although the length is very complex, the sample code is very detailed and easy to understand. If you want to know more about it, please pay attention to the industry information.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report