Python urllib2 for HTTP get,put,post,delete

Source: Internet
Author: User

#GET:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
import Urllib2
def ():
    URL = ' www.baidu.com '   #页面的地址
     Response = Urllib2. (url) # Call Urllib2 to send a GET request to the server
    return response read () #获取服务器返回的页面信息



#POST:

#!/usr/bin/env python
#-*-Coding:utf-8-*-
Import Urllib
Import Urllib2
def Post():
Url= ' http://umbra.nascom.nasa.gov/cgi-bin/eit-catalog.cgi ' #页面的地址
Values= {' Obs_year ':' 2011 ',' Obs_month ':' March ',#post的值
' Obs_day ':' 8 ',' Start_year ':' 2011 '
,' Start_month ':' March ',' Start_day ':' 8 '
,' Start_hour ':' All Hours ',' Stop_year ':' 2011 '
,' Stop_month ':' March ',' Stop_day ':' 8 '
,' Stop_hour ':' All Hours ',' Xsize ':' All '
, ' ysize ' : ' all ' , ' wave ' : ' all '
               , ' filter ' : ' all ' , ' object ' :< span class= "str" > ' all '
               , ' xbin ' : ' all ' , ' ybin ' : ' all '     

,' HIGHC ':' All '}

Data=Urllib.UrlEncode(Values)#适用urllib对数据进行格式化编码
PrintData#输出查看编码后的数据格式
    req = Urllib2.request (url, data     #生成页面请求的完整数据
    response = Urllib2. (req     Span class= "com" > #发送页面请求
    return. Read ()     #获取服务器返回的页面信息



#PUT

Import Urllib2

Request = urllib2. Request(' http://example.org ', data=' Your_put_data ')

request. ( ' Content-type ' , ' your/contenttype ' request. = lambda:< Span class= "PLN" > ' PUT '
response =< Span class= "PLN" > urllib2. (request)

  

#DELETE

import urllib2

request = urllib2. Request (uri)
request get_method = lambda< Span class= "pun" >: ' DELETE '
response Span class= "pun" >= urllib2. (request)

Python urllib2 for HTTP get,put,post,delete

Related Article

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.