Pycurl Upload File Client

Source: Internet
Author: User

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! "

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.