zoukankan      html  css  js  c++  java
  • python 模拟get,post,delete,put请求应该怎么写

    方法一:

    使用httplib

    import httplib

    hostname="localhost"

    port=80

    method="get" #get,post,put,delete

    url="/api/userinfo/get"

    body_param='{"uid":3}'

    httpheader_json={"content-Type":"application/json","Accept":"application/json"}

    httpClient=httplib.HTTPConnection(hostname,port)

    httpClient.request(method,url,body_param,httpheader_json)

    response=httpClient.getresponse()

    status=response.status

    reason=response.reason

    response_header=response.getheaders()

    response_body=response.read()

    方法二:

    使用urllib2

    import urllib2

    url="http://localhost"

    param='{"userid":1}'

    request=urllib2.Request(url.param)

    request.add_header("Content-Type","application/json")

    request.add_header("Accept","application/json")

    request.get_method=lambda :"GET"#"GET,POST,PUT,DELETE"

    response=urllib2.urlopen(request)

    response_txt=response.read()

    response_header=response.info()

  • 相关阅读:
    Linux基础命令mv
    Linux基础命令cp
    闭包函数
    函数的嵌套
    函数对象
    global与nonlocal
    名称空间与作用域
    函数的参数(总结)
    函数的基本使用
    文件的操作之指针移动
  • 原文地址:https://www.cnblogs.com/mylele/p/5015357.html
Copyright © 2011-2022 走看看