zoukankan      html  css  js  c++  java
  • 用Python发生RestFul API POST和GET请求

    使用Python调Restful API

    本文给出用GET方法从Restful API获取信息和用POST方法向Restful API发生消息。主要使用的包是urllib和json,其中urllib用来发送http请求,json包用来解析和构造json数据。具体例子如下:

    通过GET方法获取信息

    import json
    from urllib import request
    
    query_url_addr='' #the Restful api url
    query_headers={'cookie':'the cooke'} #the request headers
    req = request.Request(query_url_addr, headers=query_headers)
    resp = request.urlopen(req)
    result = resp.read().decode()
    result_json = json.loads(result)#the json object of response data
    

    用POST方法向Restful API发生消息

    import json
    import time
    from urllib import request
    from urllib import error
    try:
        create_url=''#the create request url
        create_headers={'cookie':'the cooke'} #the request headers
        body_data_str='{"body":"bodytext"}'
        body_data = bytes(body_data_str, 'utf8')
        req = request.Request(create_url, headers=create_headers, data=body_data, method='POST')
        resp = request.urlopen(req)
        result = resp.read().decode()
        result_json = json.loads(result)
        return result_json
    except error.HTTPError as err:
         error_body = err.file.read().decode()
         return  json.loads(result)
    
  • 相关阅读:
    clickhouse群集模式搭建
    基于Att&ck模型的整体威胁框架方法论
    应急响应Windows各种操作记录备份
    代理总结
    Linux应急响应日志分析
    Web漏洞利用框架
    Suricata策略记录
    应急响应汇总
    IDS&IPSSuricata介绍
    ATT&CKMitreInitial Access(初始化访问)
  • 原文地址:https://www.cnblogs.com/dereklovecc/p/12634202.html
Copyright © 2011-2022 走看看