Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Pycurl attributes and methods in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of Pycurl properties and methods in Python, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Pycurl package is a Python interface of libcurl, written in C language, powerful and fast. Since there are too many properties and methods in pycurl, write this blog post to record the properties and methods of pycurl.

Normal installation

Pip install pycurl

If there is a problem, you can search for the installation method by system version, such as centos7.1 to install pycurl

General request method import pycurl,urllibfrom io import BytesIO url = 'http://www.baidu.com' headers = ["User-Agent:Mozilla/5.0 (iPhone) CPU iPhone OS 5: 0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3 ",] data = {" cityListName ":", "trade": "} c = pycurl.Curl () # construct an object # c.setopt (pycurl.REFERER, 'http://www.baidu.com/') # set refererc.setopt (pycurl.FOLLOWLOCATION) through the curl method True) # automatically jump grab c.setopt (pycurl.MAXREDIRS,5) # set the maximum number of jumps c.setopt (pycurl.CONNECTTIMEOUT, 60) # set link timeout c.setopt (pycurl.TIMEOUT,120) # download timeout c.setopt (pycurl.ENCODING, 'gzip,deflate') # process gzip content # c.setopt (c.PROXY Ip) # proxy c.fp = BytesIO () c.setopt (pycurl.URL, url) # set the URLc.setopt (pycurl.HTTPHEADER,headers) to be accessed # incoming request header c.setopt (pycurl.POST, 1) c.setopt (pycurl.POSTFIELDS, urllib.urlencode (data)) # incoming POST data c.setopt (c.WRITEFUNCTION C.fp.write) # callback write string cache c.perform () code = c.getinfo (c.HTTP_CODE) # return status code html = c.fp.getvalue () # return source code print c.getinfo (c.TOTAL_TIME) GET request method c = pycurl.Curl () # construct an object c.setopt (pycurl.FOLLOWLOCATION) through the curl method True) # automatically jump grab c.setopt (pycurl.MAXREDIRS,5) # set the maximum number of jumps c.setopt (pycurl.CONNECTTIMEOUT, 60) # set link timeout c.setopt (pycurl.TIMEOUT,120) # download timeout c.setopt (pycurl.ENCODING, 'gzip,deflate') # process gzip content # c.setopt (c.PROXY Ip) # proxy c.fp = BytesIO () c.setopt (pycurl.URL, url) # set the URLc.setopt (pycurl.USERAGENT,ua) to be accessed # incoming ua# c.setopt (pycurl.HTTPHEADER,self.headers) # incoming request header c.setopt (c.WRITEFUNCTION C.fp.write) # callback write string cache c.perform () code = c.getinfo (c.HTTP_CODE) # return status code html = c.fp.getvalue () # return source code POST request method c = pycurl.Curl () # construct an object c.setopt (pycurl.FOLLOWLOCATION, True) # automatically jump fetch c.setopt (pycurl.MAXREDIRS) 5) # set the maximum number of redirects to c.setopt (pycurl.CONNECTTIMEOUT, 60) # set link timeout c.setopt (pycurl.TIMEOUT,120) # download timeout c.setopt (pycurl.ENCODING, 'gzip,deflate') # process gzip content # c.setopt (c.PROXY c.setopt IP) # proxy c.fp = BytesIO () c.setopt (pycurl.URL Url) # set the URLc.setopt (pycurl.USERAGENT,ua) # pass in User-Agent# c.setopt (pycurl.HTTPHEADER,headers) # pass in the request header c.setopt (pycurl.POST, 1) c.setopt (pycurl.POSTFIELDS, urllib.parse.urlencode (data)) c.setopt (c.WRITEFUNCTION C.fp.write) # callback write string cache c.perform () code = c.getinfo (c.HTTP_CODE) # return status code html = c.fp.getvalue () # return source code windows access https

The method for windows to access https requires a certificate

Import certific.setopt (pycurl.CAINFO, certifi.where ()) gets the address of the page after multiple redirects c.getinfo (pycurl.EFFECTIVE_URL) gets the final address record of the page Cookiec.setopt (pycurl.COOKIEFILE, "cookie_file_etherscan") # reads cookiec.setopt (pycurl.COOKIEJAR, "cookie_file_etherscan") # sets other properties of cookie

Partial API of pycurl:

Pycurl.Curl () # method to create a pycurl object pycurl.Curl (pycurl.URL, http://www.google.com.hk) # set the URLpycurl.Curl (). Setopt (pycurl.MAXREDIRS, 5) # set the maximum number of redirects pycurl.Curl (). Setopt (pycurl.CONNECTTIMEOUT, 60) pycurl.Curl (). Setopt (pycurl.TIMEOUT, 300) # connection timeout setting pycurl.Curl () .setopt (pycurl.USERAGENT, "Mozilla/4.0 (compatible)" MSIE 6.0; Windows NT 5.1; SV1 .net CLR 1.1.4322) ") # Analog browser pycurl.Curl () .perform () # Information returned by the server pycurl.Curl () .getinfo (pycurl.HTTP_CODE) # View the status of HTTP similar to the status attribute pycurl.NAMELOOKUP_TIME domain name resolution time in urllib pycurl.CONNECT_TIME remote server connection time pycurl.PRETRANSFER_TIME connection time pycurl.STARTTRANSFER_TIME reception up to the start of transmission Time to the first byte pycurl.TOTAL_TIME Total time of the last request pycurl.REDIRECT_TIME if there is a turn Time spent pycurl.HTTP_CODE HTTP response code pycurl.REDIRECT_COUNT redirection number of times pycurl.SIZE_UPLOAD uploaded data size pycurl.size _ DOWNLOAD downloaded data size pycurl.speed _ UPLOAD upload speed pycurl.HEADER_SIZE header size pycurl.REQUEST_SIZE request size pycurl.CONTENT_LENGTH_DOWNLOAD download content length pycurl.CONTENT_LENGTH_UPLOAD upload content length pycurl.CONTENT_TYPE content type pycurl.RESPONSE_CODE response Code pycurl.SPEED_DOWNLOAD download speed time information of pycurl.INFO_FILETIME file pycurl.HTTP_CONNECTCODE HTTP connection code above is all the content of the article "sample Analysis of Pycurl Properties and methods in Python" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report