New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mookoo

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mookoo

Mock HTTP server

  • 0.1.0
  • PyPI
  • Socket score

Maintainers
1

MooKoo

MooKoo是一个基于Python的Mock http server

安装

使用pip安装

pip install mookoo
mookoo mock1 && cd mock1 && python mock.py
# 在浏览器中访问 http://localhost:7928/hello

手工安装

mkdir mock1; cd mock1
git clone https://github.com/gaorx/mookoo.git && rm -rf mookoo/.git
vim mock.py

起步

mock1目录中,编辑mock.py

from mookoo import *
GET('/hello').json({"message": "Hello MooKoo!"})
run()

然后在shell中执行

python mock.py
# 也可以设定端口
# python mock.py -p 9928

然后在浏览器中就访问http://localhost:7928/hello,就可以看到

{"message": "Hello Mookoo"}

也可以访问http://localhost:7928/+mookoo查看此帮助文件

进阶

动态加载文件

mock1目录中,创建hello.json,然后编辑它,然后使用下面的代码动态加载

GET('/hello').load_json('hello.json')

修改StatusHeader

# 定制Status
GET('/404').html('<h1>Not found</h1>', status=404)

# 加入Header
GET('/custom_header').text("Press F12", header={"My-Header": "HeaderContent"})

# 修改content_type
GET('/custom_content_type').text("<h1>Press F12</h1>", content_type='text/html')

动态响应

@GET('/dynamic/<sub>')
def _dynamic(sub):
	response.content_type = 'text/plain'
    return "Sub path is %s, query_string is '%s'" % (sub, request.query_string)

静态文件

将一张在mock1目录中复制一张图片hello.jpg

GET('/image').static_file('hello.jpg')

静态目录

@mookoo.GET('/static/<filename:path>')
def _static_dir(filename):
    return mookoo.static_file(os.path.join('static_dir', filename))

动态JSON

mock1目录中,创建hello.json.py,内容为:

JSON = {
	"message": "Python json",
    "query_string": request.query_string,
}

然后使用下面的代码加载:

GET('/dynamic_json').load_json('hello.json.py')

Helpers

可以使用mookoo.helper注册共享函数,然后在动态JSON中可以使用helpers来使用这些函数。例如在mock.py中:

@helper
def helper1():
	return "Helper1"

@helper()
def helper2():
	return "Helper2"

@helper('helper3')
def the_name_is_not_helper3():
	return "Helper3"

GET('/dynamic_json').load_json('hello.json.py')

hello.json.py

JSON = {
	"message": "Python json",
    "query_string": request.query_string,
    "helper1": helpers.helper1(),
    "helper2": helpers.helper2(),
    "helper3": helpers.helper3(),
}

重定向

GET('/redirect').redirect('https://github.com/gaorx/mookoo')

代理

GET('/http_rfc').proxy('https://tools.ietf.org/rfc/rfc2616.txt')

注意: Proxy只会使用转发源Header中的Status和Content-Type,其余Header不转发。

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc