Common methods of Httplib and URLLIB2

Source: Internet
Author: User

These are some of the methods that have been used a few years ago and have been discarded before, and now turn them over and record them.

Import Httplibimport urllib2import socket##-------------------------------------------------------- Get request Conn=httplib with Httplib. Httpconnection (' 172.22.31.40 ', 8080) # #如果要走代理, it's natural to change the address of the proxy server conn.request ("GET", "/cloud/index.php") result= Conn.getresponse () print result.read () resultstatus=result.statusprint resultstatusconn.close () conn=httplib. Httpconnection (' www.cnblogs.com ', +) conn.request ("GET", "/idbeta/default.html") Result=conn.getresponse () print Result.read () Resultstatus=result.statusprint resultstatusconn.close () # #---------------------------------------- ----------------POST request conn=httplib with Httplib. Httpconnection (' 172.22.131.40 ', #) # #一般重点关注真实包的 "Content-type", such as "application/x-www-form-urlencoded", etc. While data is modeled according to the actual packet-grabbing situation, it requires URL-encoded Urllib.urlencodeheader1 = {"Content-type": "Application/x-www-form-urlencoded", " User-agent ":" Test "} data1 = ' ' {" audit_control_list ": []," WS ": [" base_setting ",]," Xp_fix ": []}" u Rl1= '/api/getconf.json?mid=ebcd32d5f68e404db1ccc8ff2dacb360&ver=1.0 ' conn.request (' POST ', Url1,body=data1,headers=header1) result=conn.getresponse () Content=result.read () print Contentconn.close () # #发送multipart example of/form-data request conn=httplib. Httpconnection (' 172.22.131.40 ', ") header2={" Content-type ":" Multipart/form-data; boundary=----------------------------2bb6caed7d98 "}data2 ="------------------------------ 2bb6caed7d98content-disposition:form-data; Name= "em" md5s=92f44e405db16ac55d97e3bfe3b132fa+04c5d2b4da9a0f3fa8a45702d4256cee42d8c48d452608\windows\syswow64 \windowspowershell\v1.0\powershell.exe1dcca4b04af87e52ef9eaa2190e06cbac+ 12a602b86fc394b1c88348fb099685eabb8764951174016\program Files\Windows Sidebar\ Sidebar.exe1------------------------------2bb6caed7d98--' url2= '/cloudquery.php ' conn.request ("POST", Url2,body =data2,headers=header2) result=conn.getresponse () resultstatus=result.status# #获取请求的页面内容content =result.read () Print Contentprint result.status,result.reason# #关闭连接conn. Close () # #---------------------------------------------- ----------make a GET request with Urllib2 #Direct Open is geturl= "http://www.cnblogs.com/idbeta/default.html" response =urllib2.urlopen (URL) print Response.read () 
# #--------------------------------------------------------POST request with Urllib2 url= "http://172.22.131.40/api/ getconf.json?mid=ebcd32d5f68e404db1ccc8ff2dacb360&ver=1.0 "header1 = {" Content-type ":" application/ X-www-form-urlencoded "," user-agent ":" Test "} data1 =" {"Audit_control_list": [], "base_config": [], "Data_lin Kage ": []," MD ": []," nac_linkage ": []," Neteye ": []," p2p_setting ": []," safe_control_list ": []," SD ": ["Sd_settings", "rp_settings"], "UI": [], "WS": ["base_setting", "popwnd_setting", "startup_assis Tant "," safe_protect "," Leak_repair "]," Xp_fix ": []} ' req = Urllib2. Request (URL, data1, header1) # Send the data form at the same time, this is the dictionary mode response = Urllib2.urlopen (req) #接受反馈的信息the_page = Response.read () #读取反馈的内容print the_page##--------------------------------------------------------Urllib2 Set time-out # #方法一 Global Settings Socket.setdefaulttimeout (1); s # #或urllib2. Socket.setdefaulttimeout (1) # #方法二 Urllib2.urlopen Add Timeout parameter Urllib2.urlopen(url,timeout=1) # # #--------------------------------------------------------URLLIB2 Set Agent proxy = Urllib2. Proxyhandler ({"http": ' http://172.22.31.85:808 '}) Urllib2.install_opener (Urllib2.build_opener (proxy)) url= "http:/ /www.cnblogs.com/idbeta/default.html "response =urllib2.urlopen (URL) print Response.read ()

In addition to the above mentioned, there are HTTPLIB2, Pycurl, requests, and so on are related to the HTTP, the use of the same, we go to their respective official website to see the introduction on it, it can be seen Python third-party library is a bit too much ah.

Common methods of Httplib and URLLIB2

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.