Directory:
Config.ini
[local_environment]title = 本地测试环境url = http://192.168.100.28/login_url= http://192.168.100.28/users/login/user_url = http://192.168.100.28/users/user/user_list_url= http://192.168.100.28/api/users/v1/users/user_create_url = http://192.168.100.28/users/user/create/username = adminpassword = admin
jumpserver.py
#!/usr/bin/env python3#-*-coding:utf-8-*-import osimport configparserimport socketimport requestsimport Jsonimport re Class Jumpserver (object): Def __init__ (self): Self.header = {' Content-type ': ' application/x-www-form-urlencoded ' } self.lgconf = self.load_conf () Self.r_session = requests. Session () Self.reg = r "<input type= ' hidden ' name= ' Csrfmiddlewaretoken ' value= ' (. +) '/> ' def load_conf (self ): Conf_path = Os.path.join (Os.path.dirname (Os.path.abspath (__file__)), "Config.ini") If not os.path.exists ( Conf_path): Raise Exception ("config file is not exists! Please filling configuration file ") conf = Configparser. Configparser () conf.read (conf_path) Configmodel = ' product_environment ' if Socket.gethostname (). EndsWith (' PR Oduct.com ') Else ' local_environment ' if not conf.has_section (Configmodel): Raise Exception ("there Configuration file%s Configuration items "% (Configmodel)) For para in (' URLs ', ' username ', ' password '): If Not Dict (Conf.items (Configmodel)). Get (para): RA Ise Exception ("There is no '%s ' parameter in the configuration item%s"% (Para, Configmodel)) return Dict (conf.ite MS (Configmodel)) def login (self): Try:request = Self.r_session.get (Self.lgconf.get (' Login_url '), hea Ders=self.header, timeout=20) csrf, r_sessions = Re.findall (Self.reg, Request.text) [0], Self.r_session Auth = {' username ': self.lgconf.get (' username '), ' Password ': self.lgconf.get (' password '), ' CSRFMIDDL Ewaretoken ': csrf} login_request = R_sessions.post (Self.lgconf.get (' Login_url '), Data=auth, Headers=self.header , timeout=2, allow_redirects=true) if Login_request.url = = Self.lgco Nf.get (' URL '): Return True Else:return False except Exception as E: Print ("Request URL failed:%s "% (e)) return def user_list (self): Try:request = Self.r_session.get (Self.lgco Nf.get (' User_list_url '), Headers=self.header, timeout=20) return Json.loads (request.text) except Excepti On as E:print ("Request url failed:%s"% (e)) return def user_create (self, data): try: Request = Self.r_session.get (Self.lgconf.get (' User_create_url '), Headers=self.header, timeout=20) data[ ' Csrfmiddlewaretoken '] = Re.findall (Self.reg, Request.text) [0] user_create_request = self.r_session.post (self.l Gconf.get (' User_create_url '), Data=data, Headers=self.header, TIMEOUT=20, allow_redirects=true) if User_create_request.url = = s Elf.lgconf.get (' User_url '): Return True Else:return False except Exceptio N as E:print("Request url failed:%s"% (e)) return False def logout (self): Try:request = Self.r_sessi On.get (Self.lgconf.get (' Logout_url '), Headers=self.header, timeout=20, Allow_redir ects=true) If Request.status_code = = 200:return True else:return Fal Se except Exception as e:print ("Logout url failed:%s"% (e)) returnif __name__ = = "__main__" : Http_request = Jumpserver () http_request.login () User_list = Http_request.user_list () ret = Http_request.use R_create (data={' name ': ' Hequan ', ' username ': ' Hequan ', ' email ': ' [email protected] ', ' otp_level ': 0, ' role ': ' User ', ' date_expired ': ' 2088-08-02 '}) http_request.logout ()
Python emulation login Django Project CSRF (jumpserver example)