python - 这是用 Python 编写 POST 函数的正确方法吗?

标签 python http post

def URLRequest(url, params, method="POST"):
    h = Http()
    res, content = h.request(url, method, urllib.urlencode(params))
    r = {}
    r['res'] = res
    r['content'] = content
    return r

假设我想将 {"key":"value"} 发布到一个 url (REST)。这个功能是正确的做法吗?

最佳答案

如果有效,那就是正确的。不过,您可以缩短它:

def URLRequest(url, params, method="POST"):
    res, content = Http().request(url, method, urllib.urlencode(params))
    return {'res':res, 'content':content}

关于python - 这是用 Python 编写 POST 函数的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483032/

相关文章:

c# - 统一: POST request using WWW class using JSON

post - *如何* Powerbuilder POST 工作?

python - 尝试在 Eclipse 中运行 Python 2.7 和 3.4 时遇到问题

Python:查找我正在使用的函数版本

python-3.x - 使用 Python 模块 aiohttp 捕获 http 错误连接的正确方法是什么?

c# - 如何等待服务器成功响应 HTTP 请求

http - 使用 HTTP 恢复上传?

python - 线性回归 - 均方误差太大

python - 用 Python 分解三项式

jquery - 访问函数外的值