cURL là một công cụ được sử dụng để truyền dữ liệu đến và từ một máy chủ và để thực hiện các loại yêu cầu dữ liệu khác nhau. Trong bài viết này mình sẽ giới thiệu về PycURL – một thư viện cURL rất tốt của Python. Show
Cài đặt PycURLĐể cài đặt thư viện PycURL trong python chúng ta sử dụng công cụ quản lý PIP pip install pycurl Nếu bạn gặp phải lỗi: error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1, thì tham khảo bài viết này để fix lỗi. Sử dụng PycURLMột ví dụ sử dụng PycURL Lấy nội dung website sử dụng PycULR#! /usr/bin/env python # -*- coding: utf-8 -*- # vi:ts=4:et import sys import pycurl PY3 = sys.version_info[0] > 2 class Test: def __init__(self): self.contents = '' if PY3: self.contents = self.contents.encode('ascii') def body_callback(self, buf): self.contents = self.contents + buf sys.stderr.write("Testing %s\n" % pycurl.version) t = Test() c = pycurl.Curl() c.setopt(c.URL, 'https://vinasupport.com') c.setopt(c.WRITEFUNCTION, t.body_callback) c.perform() c.close() print(t.contents) Upload file với PycURL#! /usr/bin/env python # -*- coding: utf-8 -*- # vi:ts=4:et import os, sys import pycurl # Class which holds a file reference and the read callback class FileReader: def __init__(self, fp): self.fp = fp def read_callback(self, size): return self.fp.read(size) # Check commandline arguments if len(sys.argv) < 3: print("Usage: %s <url> <file to upload>" % sys.argv[0]) raise SystemExit url = sys.argv[1] filename = sys.argv[2] if not os.path.exists(filename): print("Error: the file '%s' does not exist" % filename) raise SystemExit # Initialize pycurl c = pycurl.Curl() c.setopt(pycurl.URL, url) c.setopt(pycurl.UPLOAD, 1) # Two versions with the same semantics here, but the filereader version # is useful when you have to process the data which is read before returning if 1: c.setopt(pycurl.READFUNCTION, FileReader(open(filename, 'rb')).read_callback) else: c.setopt(pycurl.READFUNCTION, open(filename, 'rb').read) # Set size of file to be uploaded. filesize = os.path.getsize(filename) c.setopt(pycurl.INFILESIZE, filesize) # Start transfer print('Uploading file %s to url %s' % (filename, url)) c.perform() c.close() Post form với PyCURL#! /usr/bin/env python # -*- coding: utf-8 -*- # vi:ts=4:et import pycurl try: # python 3 from urllib.parse import urlencode except ImportError: # python 2 from urllib import urlencode c = pycurl.Curl() c.setopt(c.URL, 'https://httpbin.org/post') post_data = {'field': 'value'} # Form data must be provided already urlencoded. postfields = urlencode(post_data) # Sets request method to POST, # Content-Type header to application/x-www-form-urlencoded # and data to send in request body. c.setopt(c.POSTFIELDS, postfields) c.perform() c.close() Và rất nhiều ví dụ khác, các bạn vui lòng tham khảo ở đây Nguồn: vinasupport.com Hướng dẫn html tag example - ví dụ về thẻ htmlTrang chủTham khảoTag HTML/XHTMLTag HTML/XHTMLDTD cho biết: thuộc tính HTML 4.01 / XHTML 1.0 DTD nào được cho phép. T=Transitional, S=Strict, và F=Frameset. T=Transitional, S=Strict, ... Hướng dẫn dùng mongodb match pythonLời nói đầu.Xin chào mọi người đã quay trở lại seria bài viết về python của mình . Ai cần đọc về bài viết về python phần 1, 2 và 3 của mình thì click ... Hướng dẫn ipython shell - vỏ ipythonIPython là một Python shell mạnh mẽ xử lý thụt lề, tô sáng cú pháp, tự động hoàn thành cú pháp, v.v. Là một công cụ cần thiết khi lập trình với Python, ... Hướng dẫn tài liệu nodejsNhắc tới lập trình, ta không thể bỏ qua NodeJS – một nền tảng viết bằng JavaScript giúp xây dựng các ứng dụng mạng tốc độ nhanh chóng. Những tài liệu ... Hướng dẫn mongodb create - mongodb tạoMột số định nghĩa trước khi đi sâu về MongoDB. Đây là một cross-platform, DB hướng documents với hiệu năng cao, có thể đáp ứng cho đa dạng các hệ thống và ... |