Upload a file using pycurl
#! /Usr/bin/ENV Python
#-*-Coding: UTF-8 -*-
Import pycurl
Import stringio
C = pycurl. Curl () # create a curl object corresponding to the curl processor in the same libcurl
Fp = stringio. stringio ()
C. setopt (pycurl. writefunction, FP. Write)
C. setopt (pycurl. followlocation, 1)
C. setopt (pycurl. maxredirs, 5)
C. setopt (pycurl. connecttimeout, 60)
C. setopt (pycurl. Timeout, 300)
C. setopt (C. Post, 1)
C. setopt (C. url, "http: // localhost: 8080/upload. Action") # Set the URL to be accessed
# Set the POST request to upload the file's field name.
C. setopt (C. httppost, [("thefile", (C. form_file, "/home/Ubuntu/avatar.jpg")])
C. Perform () # perform the above operations to access the website
C. Close () # When no operation is performed on the curl object, the close operation is automatically executed.
Print "the python shell over! "
Pycurl POST request
#! /Usr/bin/ENV Python
#-*-Coding: UTF-8 -*-
Import pycurl
Import stringio
Post_data_dic = {"MD5": MD5, "DESC": "The file MD5 "}
C = pycurl. Curl () # create a curl object corresponding to the curl processor in the same libcurl
Fp = stringio. stringio ()
C. setopt (pycurl. writefunction, FP. Write)
C. setopt (pycurl. followlocation, 1)
C. setopt (pycurl. maxredirs, 5)
C. setopt (pycurl. connecttimeout, 60)
C. setopt (pycurl. Timeout, 300)
C. setopt (C. Post, 1)
C. setopt (C. url, "http: // localhost: 8080/post. Action") # Set the URL to be accessed
C. setopt (C. postfields, urllib. urlencode (post_data_dic ))
C. Perform () # perform the above operations to access the website
C. Close () # When no operation is performed on the curl object, the close operation is automatically executed.
Print "the python shell over! "