【发布时间】:2011-08-09 04:28:21
【问题描述】:
我需要一个实时测试服务器,它通过 HTTP GET 接受我对基本信息的请求,还允许我发布(即使它真的什么也没做)。这完全是为了测试目的。
一个很好的例子是here。它很容易接受 GET 请求,但我需要一个也接受 POST 请求的。
有人知道我也可以发送虚拟测试消息的服务器吗?
【问题讨论】:
-
你想让它记录 POST 吗?
我需要一个实时测试服务器,它通过 HTTP GET 接受我对基本信息的请求,还允许我发布(即使它真的什么也没做)。这完全是为了测试目的。
一个很好的例子是here。它很容易接受 GET 请求,但我需要一个也接受 POST 请求的。
有人知道我也可以发送虚拟测试消息的服务器吗?
【问题讨论】:
它与您请求中使用的任何这些类型的数据相呼应:
【讨论】:
$ pip install httpbin gunicorn && gunicorn httpbin:app 提到的是httpbin.org
curl -iX POST httpbin.org/post 它返回 200。
“在这里你会找到一个服务器,它接收你希望给它的任何 POST 并存储内容供你查看。”
【讨论】:
Webhook Tester 是一个很棒的工具:https://webhook.site (GitHub)
对我来说很重要,它显示了请求者的 IP,这在您需要将 IP 地址列入白名单但不确定它是什么时很有帮助。
【讨论】:
http://requestb.in 与前面提到的工具类似,并且用户界面也非常漂亮。
RequestBin 为您提供一个 URL,该 URL 将收集对其发出的请求,并让您以一种人性化的方式检查它们。 使用 RequestBin 查看您的 HTTP 客户端发送的内容或检查和调试 webhook 请求。
虽然它已于 2018 年 3 月 21 日停产。
我们已经停止了 RequestBin 的公共托管版本,原因是持续存在滥用行为,这使得网站的可靠运行变得非常困难。请参阅instructions 以设置您自己的自托管实例。
【讨论】:
如果您想要一个接受任何 URL 并将请求转储到控制台的本地测试服务器,您可以使用 node:
const http = require("http");
const hostname = "0.0.0.0";
const port = 3000;
const server = http.createServer((req, res) => {
console.log(`\n${req.method} ${req.url}`);
console.log(req.headers);
req.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello World\n");
});
server.listen(port, hostname, () => {
console.log(`Server running at http://localhost:${port}/`);
});
将其保存在文件“echo.js”中并按如下方式运行:
$ node echo.js
Server running at http://localhost:3000/
然后您可以提交数据:
$ curl -d "[1,2,3]" -XPOST http://localhost:3000/foo/bar
这将显示在服务器的标准输出中:
POST /foo/bar
{ host: 'localhost:3000',
'user-agent': 'curl/7.54.1',
accept: '*/*',
'content-length': '7',
'content-type': 'application/x-www-form-urlencoded' }
BODY: [1,2,3]
【讨论】:
看看PutsReq,它与其他类似,但它也允许您使用 JavaScript 编写您想要的响应。
【讨论】:
nc单线本地测试服务器
Linux下单行搭建本地测试服务器:
nc -kdl localhost 8000
另一个 shell 上的示例请求生成器:
wget http://localhost:8000
然后在第一个 shell 上你看到请求出现了:
GET / HTTP/1.1
User-Agent: Wget/1.19.4 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: localhost:8000
Connection: Keep-Alive
netcat-openbsd 软件包中的nc 在 Ubuntu 上广泛可用并预安装。
在 Ubuntu 18.04 上测试。
【讨论】:
nc -kdl localhost 8000 将循环监听,因此不需要 bash while。但是,nc 不会响应,因此测试查询将等到超时才响应。
while true; do echo -e "HTTP/1.1 200 OK\n" | nc -Nl 8000; done 每次都会让 nc 响应一个 200 OK 代码。
这是一个 Postman 回声:https://docs.postman-echo.com/
示例:
curl --request POST \
--url https://postman-echo.com/post \
--data 'This is expected to be sent back as part of response body.'
回复:
{"args":{},"data":"","files":{},"form":{"This is expected to be sent back as part of response body.":""},"headers":{"host":"postman-echo.com","content-length":"58","accept":"*/*","content-type":"application/x-www-form-urlencoded","user-agent":"curl/7.54.0","x-forwarded-port":"443","x-forwarded-proto":"https"},"json":{"...
【讨论】:
您可以在本地运行实际的 Ken Reitz's httpbin 服务器(在 docker 下或裸机上):
https://github.com/postmanlabs/httpbin
docker pull kennethreitz/httpbin
docker run -p 80:80 kennethreitz/httpbin
## install dependencies
pip3 install gunicorn decorator httpbin werkzeug Flask flasgger brotlipy gevent meinheld six pyyaml
## start the server
gunicorn -b 0.0.0.0:8000 httpbin:app -k gevent
现在您的个人 httpbin 实例在 http://0.0.0.0:8000 上运行(对您的所有 LAN 可见)
我想要一个返回预定义响应的服务器,所以我发现在这种情况下使用最小的 Flask 应用程序更简单:
#!/usr/bin/env python3
# Install dependencies:
# pip3 install flask
import json
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def root():
# spit back whatever was posted + the full env
return jsonify(
{
'request.json': request.json,
'request.values': request.values,
'env': json.loads(json.dumps(request.__dict__, sort_keys=True, default=str))
}
)
@app.route('/post', methods=['GET', 'POST'])
def post():
if not request.json:
return 'No JSON payload! Expecting POST!'
# return the literal POST-ed payload
return jsonify(
{
'payload': request.json,
}
)
@app.route('/users/<gid>', methods=['GET', 'POST'])
def users(gid):
# return a JSON list of users in a group
return jsonify([{'user_id': i,'group_id': gid } for i in range(42)])
@app.route('/healthcheck', methods=['GET'])
def healthcheck():
# return some JSON
return jsonify({'key': 'healthcheck', 'status': 200})
if __name__ == "__main__":
with app.test_request_context():
app.debug = True
app.run(debug=True, host='0.0.0.0', port=8000)
【讨论】:
alias httpbin='docker run -p 80:80 kennethreitz/httpbin' ?
https://www.mockable.io。它具有无需登录即可获取端点的不错功能(24小时临时帐户)
【讨论】:
创建选择一个免费的虚拟主机并输入以下代码
<h1>Request Headers</h1>
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "<b>$header:</b> $value <br />\n";
}
?>
【讨论】:
我创建了一个开源的可破解本地测试服务器,您可以在几分钟内运行它。您可以创建新的 API,定义自己的响应并以任何您希望的方式对其进行破解。
Github 链接:https://github.com/prabodhprakash/localTestingServer
【讨论】:
我不知道为什么这里的所有答案都让一个非常简单的工作变得非常困难!
当有 HTTP 请求时,实际上客户端会发送一个 HTTP_MESSAGE 到服务器 (read about what is a HTTP_MESSAGE),您可以在 只需 2 个简单步骤 中创建一个服务器:
1.安装 netcat:
在许多基于unix的系统中你已经安装了这个,如果你有windows就google一下,安装过程真的很简单,你只需要一个nc.exe文件,然后你应该复制这个nc.exe的路径文件到您的路径环境变量并检查是否每个想法都可以使用nc -h
2。创建一个正在监听 localhost:12345 的服务器:
只需在终端上输入nc -l -p 12345,一切就完成了! (在mac中nc -l 12345tnxSilvio Biasiol)
现在您有一个正在监听http://localhost:12345 的服务器,如果您是 js 开发人员,请使用 axios.post('http://localhost:12345', { firstName: 'Fred' }) 发送帖子请求,或者创建自己的 xhr,或者在 html 文件中创建表单并将其提交到服务器,像<form action="http://localhost:12345" method="post"> 或使用curl 或wget 等发出请求然后检查你的终端,你的终端上应该会出现一个原始的HTTP_MESSAGE,你可以开始你的快乐黑客;)
【讨论】:
nc -l 12345
您可能不需要任何网站,只需打开浏览器,按F12 访问开发者工具> 控制台,然后在控制台中编写一些 JavaScript 代码即可。
在这里我分享一些实现这一目标的方法:
对于 GET 请求: *.使用 jQuery:
$.get("http://someurl/status/?messageid=597574445", function(data, status){
console.log(data, status);
});
对于 POST 请求:
$.ajax:var url= "http://someurl/",
api_key = "6136-bc16-49fb-bacb-802358",
token1 = "Just for test",
result;
$.ajax({
url: url,
type: "POST",
data: {
api_key: api_key,
token1: token1
},
}).done(function(result) {
console.log("done successfuly", result);
}).fail(function(error) {
console.log(error.responseText, error);
});
var merchantId = "AA86E",
token = "4107120133142729",
url = "https://payment.com/Index";
var form = `<form id="send-by-post" method="post" action="${url}">
<input id="token" type="hidden" name="token" value="${merchantId}"/>
<input id="merchantId" name="merchantId" type="hidden" value="${token}"/>
<button type="submit" >Pay</button>
</div>
</form> `;
$('body').append(form);
$("#send-by-post").submit();//Or $(form).appendTo("body").submit();
`var api_key = "73736-bc16-49fb-bacb-643e58",
recipient = "095552565",
token1 = "4458",
url = 'http://smspanel.com/send/';`
``var form = `<form id="send-by-post" method="post" action="${url}">
<input id="api_key" type="hidden" name="api_key" value="${api_key}"/>
<input id="recipient" type="hidden" name="recipient" value="${recipient}"/>
<input id="token1" name="token1" type="hidden" value="${token1}"/>
<button type="submit" >Send</button>
</div>
</form>`;``
document.querySelector("body").insertAdjacentHTML('beforeend',form);
document.querySelector("#send-by-post").submit();
var url = "https://Payment.com/index";
Response.Clear();
var sb = new System.Text.StringBuilder();
sb.Append("<html>");
sb.AppendFormat("<body onload='document.forms[0].submit()'>");
sb.AppendFormat("<form action='{0}' method='post'>", url);
sb.AppendFormat("<input type='hidden' name='merchantId' value='{0}'>", "C668");
sb.AppendFormat("<input type='hidden' name='Token' value='{0}'>", "22720281459");
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");
Response.Write(sb.ToString());
Response.End();
【讨论】:
如果您需要或想要一个具有以下功能的简单 HTTP 服务器:
我在 PyPI 上已经非常出色的 SimpleHTTPAuthServer 之上构建了一个。这增加了对 POST 请求的处理: https://github.com/arielampol/SimpleHTTPAuthServerWithPOST
否则,所有其他公开可用的选项已经如此出色和强大。
【讨论】:
我不确定是否有人会这么痛苦地测试 GET 和 POST 调用。我使用了 Python Flask 模块并编写了一个函数,该函数的功能类似于 @Robert 分享的内容。
from flask import Flask, request
app = Flask(__name__)
@app.route('/method', methods=['GET', 'POST'])
@app.route('/method/<wish>', methods=['GET', 'POST'])
def method_used(wish=None):
if request.method == 'GET':
if wish:
if wish in dir(request):
ans = None
s = "ans = str(request.%s)" % wish
exec s
return ans
else:
return 'This wish is not available. The following are the available wishes: %s' % [method for method in dir(request) if '_' not in method]
else:
return 'This is just a GET method'
else:
return "You are using POST"
当我运行它时,如下所示:
C:\Python27\python.exe E:/Arindam/Projects/Flask_Practice/first.py
* Restarting with stat
* Debugger is active!
* Debugger PIN: 581-155-269
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
现在让我们尝试一些调用。我正在使用浏览器。
This is just a GET method
http://127.0.0.1:5000/method/NotCorrect
This wish is not available. The following are the available wishes:
['application', 'args', 'authorization', 'blueprint', 'charset', 'close', 'cookies', 'data', 'date', 'endpoint', 'environ', 'files', 'form', 'headers', 'host', 'json', 'method', 'mimetype', 'module', 'path', 'pragma', 'range', 'referrer', 'scheme', 'shallow', 'stream', 'url', 'values']
http://127.0.0.1:5000/method/environ
{'wsgi.multiprocess': False, 'HTTP_COOKIE': 'csrftoken=YFKYYZl3DtqEJJBwUlap28bLG1T4Cyuq', 'SERVER_SOFTWARE': 'Werkzeug/0.12.2', 'SCRIPT_NAME': '', 'REQUEST_METHOD': 'GET', 'PATH_INFO': '/method/environ', 'SERVER_PROTOCOL': 'HTTP/1.1', 'QUERY_STRING': '', 'werkzeug.server.shutdown': , 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36', 'HTTP_CONNECTION': 'keep-alive', 'SERVER_NAME': '127.0.0.1', 'REMOTE_PORT': 49569, 'wsgi.url_scheme': 'http', 'SERVER_PORT': '5000', 'werkzeug.request': , 'wsgi.input': , 'HTTP_HOST': '127.0.0.1:5000', 'wsgi.multithread': False, 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'HTTP_ACCEPT': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 'wsgi.version': (1, 0), 'wsgi.run_once': False, 'wsgi.errors': ", mode 'w' at 0x0000000002042150>", 'REMOTE_ADDR': '127.0.0.1', 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch, br'}
【讨论】:
另一个提供一些自定义且易于使用(无需安装、注册)的是https://beeceptor.com。
您创建一个端点,向它发出初始请求并可以调整响应。
【讨论】:
只需自己设置一个。将此 sn-p 复制到您的网络服务器。
回声“”; print_r($_POST); 回声“”;
只需将您想要的内容发布到该页面即可。完成。
【讨论】: