我想运行一个脚本来创建一个简单的 HTTP 服务器并为每个请求运行一个函数。
我正在使用 Python 来测试应该给我回调的 API。 目前,我正在使用http://requestb.in/测试回调,但我想直接在我的 python 脚本中测试它。
是否可以在 localhost:6548 上运行回调服务器绑定(bind)并根据请求测试某些内容?
感谢您的帮助。
最佳答案
您可以使用wsgiref
托管一个简单的 WSGI Web 应用程序。示例:
from wsgiref.simple_server import make_server
import datetime
def application(environ, start_response):
start_response('200 OK', [('Content-type', 'text/plain')])
retval = [
str(environ),
"\n",
"Hello world!\n",
str(datetime.datetime.now())
]
return retval
make_server('localhost', 6548, application).serve_forever()
这段代码创建了一个Python WSGI Web 应用程序并在 http://localhost:6548
上为该应用程序提供服务。
关于python - 运行一个小型 HTTP 服务器来检查回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13681748/