#GET:

#!/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:

#!/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'}

    data = urllib.urlencode(values)    #适用urllib对数据进行格式化编码
    print data    #输出查看编码后的数据格式
    req = urllib2.Request(URL, data)    #生成页面请求的完整数据
    response = urllib2.urlopen(req)     #发送页面请求
    return response.read()    #获取服务器返回的页面信息

#PUT

import urllib2

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

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


#DELETE

import urllib2

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

python urllib2对http的get,put,post,delete的更多相关文章

  1. python urllib2使用心得

    python urllib2使用心得 1.http GET请求 过程:获取返回结果,关闭连接,打印结果 f = urllib2.urlopen(req, timeout=10) the_page = ...

  2. python urllib2 模拟网站登陆

    python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...

  3. Python urllib2写爬虫时候每次request open以后一定要关闭

    最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...

  4. python urllib2实现http GET PUT DELETE POST的方法

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/3/11 下午8:33 # @Author : liubing # @File ...

  5. Python urllib2 调试

    #!/usr/bin/env python # coding=utf-8 __author__ = 'zhaoyingnan' import urllib import urllib2 import ...

  6. python urllib2详解及实例

    urllib2是Python的一个获取URLs(Uniform Resource Locators)的组件.他以urlopen函数的形式提供了一个非常简单的接口, 这是具有利用不同协议获取URLs的能 ...

  7. python urllib2与urllib

    1.urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL. 2.urllib模块可以提供进行urlencode的方法,该方法用于G ...

  8. python urllib2 httplib HTTPConnection

    httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn  ...

  9. python urllib2 对 http 的 get,put,post,delete

    #GET: #!/usr/bin/env python# -*- coding:utf-8 -*-import urllib2def get():    URL = 'www.baidu.com'   ...

随机推荐

  1. argv[1] 路径问题

    在看<学习opencv>一书时遇到一个小问题:函数只是通过argv传递参数来读取图片并显示,但是却一直弹出画布,没有图像. 如下:test.c # include<stdio.h&g ...

  2. linux中结构体对齐【转】

    转自:https://blog.csdn.net/suifengpiao_2011/article/details/47260085 linux中定义对齐字节 typedef struct  sdk_ ...

  3. python计算最大公约数和最小公倍数

    a=4 b=2 def gcd(a,b): return a if b==0 else gcd(b,a%b) def lcm(a,b): return a*b//gcd(a,b) print(gcd( ...

  4. CentOS如何设置终端显示字符界面区域的大小

    红框内的文字本应该在上一行后方,调了stty也不行, stty size的值变化,但显示还是没变化 后来参考http://www.jb51.net/os/RedHat/522217.html 修改 / ...

  5. Project Euler Problem8

    Largest product in a series Problem 8 Find the greatest product of five consecutive digits in the 10 ...

  6. centos7和centos6.5环境rpm方式安装mysql5.7和mysql5.6详解

    centos环境安装mysql5.7 其实不建议安装mysql5.7 语法和配置可能和以前的版本区别较大,多坑,慎入 1.yum方式安装(不推荐) a.安装mysql5.7 yum源 centos6: ...

  7. linux系统编译安装软件的通用步骤

    编译安装的步骤: 1.下载源代码,并解压     tar -xf package-version.tar.{gz|bz2|xz} 注意:展开后的目录通常为package-version 2.切换至源码 ...

  8. 使用Navicat Premium对sqlserver 2008进行表、字段及用户权限的精细化管理

    在一些特殊的业务场景,我们需要对数据库进行精细化的管理,比如只能授权给某用户某个表的操作权限,最小权限法则可以保障数据库最大的安全.利用navicat这个轻量化的工具可以很轻松的解决这个问题 1.通过 ...

  9. C <string.h>常用函数介绍

    1. strcpychar *strcpy(char *destin, char *source);功能:将source指向的字符串拷到destin. int main() { ]; "; ...

  10. hdu4122

    题目很长,有点恶心,但实际上是个单调队列 没搞出来,题解 https://blog.csdn.net/lvshubao1314/article/details/46910271 #include< ...