python- Network Security Programming the next day (file operation)

Foreword

Hey pot before eating, after eating the contents learned today to write a blog as notes with ha ha!

 

 

File Operations

f = open ( "test.txt", w) directly open a file, if the file does not exist, create a file
open mode
w: opened for writing,
A: opened in append mode (EOF from the beginning, creating a new file if necessary)
r +: opened in read-write mode
w +: open (see w) in write mode
a +: opened in read-write mode (see A)
RB: open binary read mode
wb: binary opened in write mode (see w)
ab &: to open binary append mode (see A)
RB +: opened in binary read-write mode (see + R & lt)
WB +: open (see w +) binary read-write mode
ab +: opened in binary read-write mode (see a +)

fp.read ([size]) #size length read in byte units
fp.readline ([size]) # line is read, if the defined size, it is possible to return only part of a line
fp.readlines ([ size]) # each line of the file as a member of a list and returns the list. In fact, its interior is invoked through the loop readline () to achieve. If the offer size parameters, size is read the contents of the total length, that is possible to read only part of the file.
fp.write (str) # The str written to a file, write () does not add a newline after str
fp.writelines (seq) seq # contents of all written to a file (multi-line one-time write into). This function is only faithfully written not add anything after each line.
fp.close () # close the file. python will turn off automatically after a file without file, but this feature is not guaranteed, it is best to develop their own habits closed. If a file can also be manipulated to produce ValueError after closing
fp.flush () # the contents of the buffer is written to disk
fp.fileno () # returns a long integer "file tag"
fp.isatty () # if the file is a terminal device file (unix system)
fp.tell () # returns the current position mark of the file operation to the beginning of the file as an origin
fp.next () # returns the next line, and the operation flag is shifted to a file the next line. Put a file when used for ... in this sentence file, it is to call the next () function to implement traversal.
fp.seek (offset [, whence]) # playing the file offset of the marker to the operation. This offset is usually relative to the beginning of the file to calculate the generally positive. But whence If a parameter might not, whence the calculation can be expressed from the beginning, 1 indicates the current position as the origin is calculated as 0. 2 represents the end of the file to be calculated as the origin. Note that if a file is opened in a + or mode, every time the write operation, the file operation flag will automatically return to the end of the file.
fp.truncate ([size]) # the documents into a predetermined size, is cut to a default location of the current file operation mark. If the size of the file is larger than the size, according to the system without changing the file may be different, it may be up to the appropriate size with 0 files, it might be a bunch of random stuff to add.

 

Contents Operation


1. Create a directory
os.mkdir ( "File")
2. Copy the file:
shutil.copyfile ( "oldfile", "newfile") #oldfile and can only be newfile file
shutil.copy ( "oldfile", "newfile ") #oldfile only folders, newfile can be a file, it can be the destination directory
3. copy the folder:
4.shutil.copytree ( "olddir", "newdir") #olddir and newdir can only be a directory, and newdir There must be no
5. rename the file (directory)
os.rename ( "oldname", "newname") is a file or directory # this command
6. move the file (directory)
shutil.move ( "OldPos", "NewPOS ")
7. delete files
os.remove (" file ")
8. delete the directory
os.rmdir (" dir ") # only delete empty directories
shutil.rmtree (" dir ") # empty directory, the content of the directory can puncturing
9. conversion directory
os.chdir ( "path") # change path

Reference to learn: https: //www.cnblogs.com/mttnor/p/python.html

python simple web scanning path

strip () method for removing head and tail of the specified character string
sys.argv: passing parameters to achieve from an external program to the program.

 1 # coding:utf-8
 2 import requests
 3 import sys
 4 url = sys.argv[1]
 5 with open("dir.txt","r") as f:
 6         for line in f.readlines():
 7                 line=line.strip()
 8                 r=requests.get(url+line)
 9                 if r.status_code==200:
10                         print("url:"+r.url+"exist")

 

Python implementation IIS PUT vulnerability detection

 1 # coding:utf-8
 2 import requests
 3 url="http://127.0.0.1"
 4 r=requests.options(url)
 5 
 6 result=r.headers['Public']
 7 if result.find("PUT") and result.find("MOVE"):
 8     print(result)
 9     print("exist iis put vul")
10 else:
11     print("not exist")

 

Guess you like

Origin www.cnblogs.com/xhds/p/12169044.html