Program for file download, file upload.

Hello everyone, today, download a chat upload relevant documents.

Demand: by busybox build a file for the site, put some files on your site, through the program, the files on the site to download and then upload to https://pastebin.com, it must be based on user identity upload.

Analysis: The first step: How to get files from the site, the second step: how to file a user uploads

Busybox on how to use the site to build a file please refer to: https://blog.csdn.net/haofan_/article/details/78369352

See the code below:

from HTTP Import CookieJar
 from urllib.parse Import urlencode
 from urllib.request Import Request, urlopen, HTTPCookieProcessor, build_opener
 from BS4 Import BeautifulSoup 

# file download function 
DEF File (): 
    url = " XXX "  # file-site routing 
    # request headers 
    header_info = {
        " the user-- agent " : " XXXX " , # user agent 
        " the Authorization ": " XXX "  # authentication information, this field may need to verify whether the site can set this field, can be used to get the packet capture tool 
    }     
    
    R & lt = Request (URL = URL, headers = header_info) # instantiated Request object 
    the try : 
        RES the urlopen = (R & lt) # sends a request 
        return res.read () decode (). # get file contents 
    the except exception AS E:
         Print (E) # Print exception information 


# define upload function 
DEF the send_file (code): 
    
    # analog subscriber Login 
    LOGIN_URL = " https://pastebin.com/login "    # Login routing 
    # login form data
    form_data = {
        "submit_hidden":"submit_hidden",
        "user_name":"XXX",
        "user_password":"XXX",
        "submit":"Login"
    }
    #构建请求头信息
    login_header = {
        "User-Agent":"XXX"The cookie management tools#   
    }
        
    
    
    = CJ cookiejar.CookieJar () 
    Handler = HTTPCookieProcessor (CJ) 
    opener = - the build_opener (Handler) 

    # using the transmission request carrying the opener Cookie 
    the params = urlencode (form_data) # form data encoded 
    login_r = Request (login_url, data = params.encode () , headers = login_header) # instantiation of the request object 
    login_res = opener.open (login_r) # send a request 
    
    iF login_res.url == " XXX " : # via url determine whether the user login is successful 
        # upload 
        # get token upload page 
        token_url = " XXX " ,
        token_res = opener.open (TOKEN_URL) # sends a request 
        token_html = the BeautifulSoup (token_res, ' lxml ' ) 
        token = STR (token_html.select ( ' [name = csrf_token_post] ' ) [0]). Split ( " value " ) [. 1 ] [2: -3] # give token 

        # build file upload form data 
        file_form_data = {
          " csrf_token_post " : token,
          " submit_hidden " : " submit_hidden " ,
          " paste_code": code,
         "paste_format": "1",
         "paste_expire_date": "N",
         "paste_private": "0",
         "paste_name": "my_code",   
        }
        #构建请求头信息
        send_file_header = {
            "User-Agent":"XXX"
        }
        send_file_url = "https://pastebin.com/post.php"
        send_params = urlencode(file_form_data)
        send_r = Request(send_file_url,data = send_params.encode(),headers=send_file_header)
        send_res = opener.open(send_r)
        
        if send_res.status == 200:
            print("上传成功!")
        else:
            print("上传失败!")
    else:
        print("登录失败!") 


IF  __name__ == " __main__ " : 
    
    
    text = get_file () # call the file download function 
    the send_file (text) # call the function to upload files

 

 

 

Guess you like

Origin www.cnblogs.com/yanhonghong/p/11619555.html