current position:Home>Python development foundation summary (VII) database + FTP + character coding + source code security

Python development foundation summary (VII) database + FTP + character coding + source code security

2022-01-31 15:10:38 zhulin1028

「 This is my participation 11 The fourth of the yuegengwen challenge 15 God , Check out the activity details :2021 One last more challenge

One 、 Use of database

1、 Used by fields in the database utf8 Format encoding , But it's a question mark . This problem can be solved by specifying the coding method when querying , Just execute sql sentence :Query_Execsql(pdb, "SET NAMES 'utf8'");

Be careful , This needs to be done immediately after the connection . also , In other operations , Will always use this code . Unless changed again .

2、fetchone(): Return a record .fetchall(): Return all records .

3、 You can use a simple method to get all the records :

cur.execute(sql)

for tel, name, pwd in cur:

                      print tel, name, pwd

Two 、FTP Use

Python The standard module of ftplib You can support FTP.

Several functions :

FTP(host= '' , user= '' , passwd= '' , acct= '' ,               timeout=_GLOBAL_DEFAULT_TIMEOUT): If there is user, be Connect(); If there is also user, be login(). If you don't use these parameters , Call yourself after connect and login.

connect(self, host='', port=0, timeout=-999): If the port is not a standard port , You need to call... Manually connect.

login(user = '', passwd = '', acct = ''): land .

pwd(): Get the current working path .

cwd(path): Change the current working path .

dir(path,cb): Display the contents of the directory .cb Is the processing function of the file . Will pass to retrlines. This function can get all the contents of a directory .

retrlines(self, cmd, callback = None): Download Text File .cmd In the form of “RETR FILENAME”,callback It's a function , To process each line of a text file . Here's a question , If you use it directly file Of write Method , Will lose the newline . And there is no writeline function .

retrbinary(self, cmd, callback, blocksize=8192, rest=None): Download binaries ,cmd In the form of “RETR FILENAME”,callback It's a function , To process every block of a text file . Default size 8k, But you can change .

storlines(self, cmd, fp, callback=None): Upload text files .cmd In the form of “STOR FILENAME”.fp It's a file object , There has to be readline Method .callback: Each line transmitted , I'll call this function .

storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None): Upload binaries .cmd In the form of “STOR FILENAME”.fp It's a file object , There has to be read(num_bytes) Method . Default size 8k, But you can change .

quit(): sign out .

3、 ... and 、 The use of character encoding

encode Yes, it will Unicode Turn into str,decode Is to convert a string to Unicode. therefore , To convert a string to another format, you can :

s = ‘ chinese ’

s.decode(fromcodec).encode(tocodec)
 Copy code 

It can also be used directly :s.encode(tocodec). This is the time , It is equivalent to calling... By default decode, And the default encoding method is used .

Four 、 Source code security

  1. Python If the code is released directly , May expose the source code .

  2. One way is to use c Expand Python, To replace the core module .

  3. Another compromise is to compile the source code , Generate pyc perhaps pyo file . These things are bytecode files . May be decompiled . therefore , You may need to study Python Of pyo Generation and loading methods , To generate safer Python Bytecode . The Internet says it can be modified Python Source code opcode. Not studied .

  4. command :python -m compileall .

  5. It can also be in Python Use in :

    import compileall
    
    compileall._dir('Lib/', force=True)
    
    # Perform same compilation, excluding files in .svn directories.
    import re
    compileall._dir('Lib/', rx=re.compile('/[.]svn'), force=True)
     Copy code 

copyright notice
author[zhulin1028],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311510377146.html

Random recommended