zoukankan      html  css  js  c++  java
  • python urllib2实现http GET PUT DELETE POST的方法

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2019/3/11 下午8:33
    # @Author  : liubing
    # @File    : httpMethod.py 定义了http 常用的请求
    
    import urllib2
    import urllib
    import socket
    import json
    
    
    
    class httpMethod(object):
    
        def __init__(self):
            pass
    
        def post(self, url=None, params=None, timeout=50):
            """Post方法"""
            old_timeout = socket.getdefaulttimeout()
            socket.setdefaulttimeout(timeout)
            try:
                # POST
                if params:
                    request = urllib2.Request(url, urllib.urlencode(params))
                # GET
                else:
                    request = urllib2.Request(url)
                request.add_header('Accept-Language', 'zh-cn')
                response = urllib2.urlopen(request)
                content = response.read()
                if response.code == 200:
                    return content, True
                return content, False
            except Exception as ex:
                print ("Post 方法调用异常:%s" % ex)
                return str(ex), False
            finally:
                if 'response' in dir():
                    response.close()
                socket.setdefaulttimeout(old_timeout)
    
        def put(self, url=None, params=None, urlencode=True):
            """urlencode 表明参数是否需要被编码,如果此选项为false。传入的params 需要是字符串形式"""
            try:
                if urlencode:
                    data = urllib.urlencode(params)
                else:
                    data = params
                req = urllib2.Request(url, data)
                req.get_method = lambda: 'PUT'
                ret = urllib2.urlopen(req).read()
                return ret
            except Exception as ex:
                print("PUT 方法调用异常:%s" % ex)
               
    
        def get(self, url):
            """get方法"""
            try:
                req = urllib2.Request(url)
                ret = urllib2.urlopen(req)
                return json.load(ret)
            except Exception as ex:
                print("Get方法调用异常:%s" % ex)
               
    
        def http_del(self, url=None, params=None):
            """定义delete 方法"""
            try:
                data = urllib.urlencode(params)
                req = urllib2.Request(url, data)
                req.get_method = lambda: 'DELETE'
                ret = urllib2.urlopen(req).read()
                return ret
            except Exception as ex:
                print("DELETE 方法调用异常:%s" % ex)
               
    

      

    除特殊说明外,其余所有文章均属原创。未经允许,请勿进行转载或者其他操作 有问题欢迎留言交流
  • 相关阅读:
    廖雪峰Java10加密与安全-1数据安全-1加密与安全概念
    廖雪峰Java9正则表达式-2正则表达式进阶-6搜索和替换
    廖雪峰Java9正则表达式-2正则表达式进阶-5非贪婪匹配
    (1)结构体问题(哎,困扰很久了)
    5)蟠桃计,但是涉及一个变量初始化位置问题
    4)mysqlDB
    2)header的使用
    )ASCII比较大小
    3)大数相加问题
    2)处理用空格的字符串输入
  • 原文地址:https://www.cnblogs.com/LiuBingBlogs/p/10503180.html
Copyright © 2011-2022 走看看