http.webp
httpbin.org 这个网站能测试 HTTP 请求和响应的各种信息,比如 cookie、ip、headers 和登录验证等,且支持 GET、POST 等多种方法,对 web 开发和测试很有帮助。

它用 Python + Flask 编写,是一个开源项目。

官方网站:http://httpbin.org/

httpbin.png

Python使用requests库: 复杂的 POST 请求

通常,你想要发送一些编码为表单形式的数据——非常像一个 HTML 表单。要实现这个,只需简单地传递一个字典给 data 参数。
你的数据字典在发出请求时会自动编码为表单形式:

import requests
payload = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post('http://httpbin.org/post', data=payload)
print(r.text)
  • 返回值 json 数据

    {
    "args": {},
    "data": "",
    "files": {},
    "form": {
      "key1": [
        "value1",
        "value2"
      ]
    },
    "headers": {
      "Accept": "*/*",
      "Accept-Encoding": "gzip, deflate",
      "Content-Length": "23",
      "Content-Type": "application/x-www-form-urlencoded",
      "Host": "httpbin.org",
      "User-Agent": "python-requests/2.25.1",
      "X-Amzn-Trace-Id": "Root=1-618fd260-75b10ed47a82fac820a8c6da"
    },
    "json": null,
    "origin": "188.212.108.88",
    "url": "http://httpbin.org/post"
    }

httpbin.org 容器部署

  • 使用官方版,比较占用空间,容器大小534M
  • 使用 simonkowallik/httpbin-alpine 镜像只需要 100M

    docker run  --name httpbin  --restart=always  -d -p 80:80 simonkowallik/httpbin-alpine
    
    docker run  --name httpbin  --restart=always  -d -p 80:80 kennethreitz/httpbin
    
    REPOSITORY                     TAG       IMAGE ID       CREATED        SIZE
    simonkowallik/httpbin-alpine   latest    a7befd1a1b0e   9 months ago   99.6MB
    kennethreitz/httpbin           latest    b138b9264903   3 years ago    534MB

Python使用requests库 访问本地网站测试 HTTP 请求和响应的各种信息

>>> r = requests.post('http://192.168.1.150/post', data=payload)
>>> print(r.text)

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "key1": [
      "value1",
      "value2"
    ]
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "keep-alive",
    "Content-Length": "23",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "192.168.1.150",
    "User-Agent": "python-requests/2.25.1"
  },
  "json": null,
  "origin": "192.168.1.121",
  "url": "http://192.168.1.150/post"
}

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用 * 标注