python urllib2對http的get,put,post,delete

#GET:
python

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import urllib2
def get():
    URL = 'www.baidu.com'  #頁面的地址
    response = urllib2.urlopen(URL) #調用urllib2向服務器發送get請求
    return response.read() #獲取服務器返回的頁面信息
服務器



#POST:
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':'all'
              ,'xbin':'all','ybin':'all'   
編碼

              ,'highc':'all'}
url

    data = urllib.urlencode(values)    #適用urllib對數據進行格式化編碼
    print data    #輸出查看編碼後的數據格式
    req = urllib2.Request(URL, data)    #生成頁面請求的完整數據
    response = urllib2.urlopen(req)     #發送頁面請求
    return response.read()    #獲取服務器返回的頁面信息



#PUT
spa

import urllib2

request = urllib2.Request('http://example.org', data='your_put_data')
code

request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
response = urllib2.urlopen(request)
utf-8



#DELETE

import urllib2

request = urllib2.Request(uri)
request.get_method = lambda: 'DELETE'
response = urllib2.urlopen(request)
get

相關文章
相關標籤/搜索