未加星标

Vasudev Ram: Give your Python function a {web,CLI} hug!

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

By Vasudev Ram


Vasudev Ram: Give your Python function a {web,CLI} hug!

I came across this interesting python framework called hug recently:

www.hug.rest

Hug is interesting because it allows you to create a function in Python and then expose it via both the web and the command-line . It also does some data type validation using Python 3's annotations (not shown in my example, but see the hug quickstart below). Hug is for Python 3 only, and builds upon on the Falcon web framework (which is "a low-level high performance framework" for, among other things, "building other frameworks" :).

Here is the hug quickstart .

The hug site says it is "compiled with Cython " for better performance . It makes some claims about being one of the faster Python frameworks . Haven't checked that out.

Here is an HN thread about hug from about a year ago, with some interesting comments, in which the author of hug also participated, replying to questions by readers, explaining some of his claims, etc. Some benchmark results for hug vs. other tools are also linked to in that thread:

I tried out some of the features of hug, using Python 3.5.2 , with a small program I wrote.

Below is the test program I wrote, hug_pdp.py . The pdp in the filename stands for p sutil d isk p artitions, because it uses the psutil disk_partitions() function that I blogged about here recently:

Using psutil to get disk partition information with Python

Here is hug_pdp.py:

""" hug_pdp.py Use hug with psutil to show disk partition info via Python, CLI or Web interfaces. Copyright 2017 Vasudev Ram Web site: https://vasudevram.github.io Blog: http://jugad2.blogspot.com Product store: https://gumroad.com/vasudevram """ import sys import psutil import hug def get_disk_partition_data(): dps = psutil.disk_partitions() fmt_str = "{:<8} {:<7} {:<7}" result = {} result['header'] = fmt_str.format("Drive", "Type", "Opts") result['detail'] = {} for i in (0, 2): dp = dps[i] result['detail'][str(i)] = fmt_str.format(dp.device, dp.fstype, dp.opts) return result @hug.cli() @hug.get(examples='drives=0,1') @hug.local() def pdp(): """Get disk partition data""" result = get_disk_partition_data() return result @hug.cli() @hug.get(examples='') @hug.local() def pyver(): """Get Python version""" pyver = sys.version[:6] return pyver if __name__ == '__main__': pdp.interface.cli()

Note the use of hug decorators in the code to enable different kinds of user interfaces and HTTP methods .

Here are some different ways of running this hug-enabled program, with their outputs:

As a regular Python command-line program, using the python command:

$ python hug_pdp.py {'detail': {'0': 'C:\\ NTFS rw,fixed', '2': 'E:\\ CDFS ro,cdrom' }, 'header': 'Drive Type Opts '}

As a command-line program, using the hug command:

$ hug -f hug_pdp.py -c pdp {'detail': {'2': 'E:\\ CDFS ro,cdrom', '0': 'C:\\ NTFS rw,fixed'}, 'header': 'Drive Type Opts '}

You can see that this command gives the same output as the previous one.

But you can also run the above command with the "-c pyver" argument instead of "-c pdp ", giving:

$ hug -f hug_pdp.py -c pyver 3.5.2

(I added the pyver() function to the program later, after the initial runs with just the pdp() function , to figure out how using the hug command to run the program was different from using the python command to run it. The answer can be seen from the above output, though there is another difference too, shown below (the web interface). Next, I ran it this way:

$ hug -f hug_pdp.py

which started a web server (running on port 8000), giving this output on the console:

/#######################################################################\ `.----``..-------..``.----. :/:::::--:---------:--::::://. .+::::----##/-/oo+:-##----::::// `//::-------/oosoo-------::://. ## ## ## ## ##### .-:------./++o/o-.------::-` ``` ## ## ## ## ## `----.-./+o+:..----. `.:///. ######## ## ## ## ``` `----.-::::::------ `.-:::://. ## ## ## ## ## #### ://::--.``` -:``...-----...` `:--::::::-.` ## ## ## ## ## ## :/:::::::::-:- ````` .:::::-.` ## ## #### ###### ``.--:::::::. .:::.` ``..::. .:: EMBRACE THE APIs OF THE FUTURE ::- .:- -::` ::- VERSION 2.2.0 `::- -::` -::-` -::- \########################################################################/ Copyright (C) 2016 Timothy Edmund Crosley Under the MIT License Serving on port 8000...

Then I went to this URL in my browser:

http://localhost:8000/pdp

which gave me this browser output:

{"detail": {"0": "C:\\ NTFS rw,fixed", "2": "E:\\ CDFS ro,cdrom"}, "header": "Drive Type Opts "}

which is basically the same as the earlier command-line interface output I got.

Next I went to this URL:

http://localhost:8000/pyver

which gave me this:

"3.5.2 "

which again is the same as the earlier corresponding command-line output of the hug command.

Of course, the output from both the web and CLI interfaces is either JSON or a dict, so in a real life app, we would have to get that output and use it in some way, such as (process it further before we) format it better for human consumption. If using a javascript front-end, it can easily be done; if using the code as is with the command-line mode, we need to figure out a way to do it. The hug module may have some support for that.

What is also interesting is that when I run it this way:

http://localhost:8000/

I get this browser output:

{ "404": "The API call you tried to make was not defined. Here's a definition of the API to help you get going :)", "documentation": { "overview": "\nhug_pdp.py\nUse hug with psutil to show disk partition info \nvia Python, CLI or Web interfaces.\nCopyright 2017 Vasudev Ram\nWeb site: https://vasudevram.github.io\nBlog: http://jugad2.blogspot.com\nProduct store: https://gumroad.com/vasudevram\n", "handlers": { "/pdp": { "GET": { "usage": "Get disk partition data", "examples": [ "http://localhost:8000/pdp?drives=0,1" ], "outputs": { "format": "JSON (Javascript Serialized Object Notation)", "content_type": "application/json" } } }, "/pyver": { "GET": { "usage": "Get Python version", "examples": [ "http://localhost:8000/pyver" ], "outputs": { "format": "JSON (Javascript Serialized Object Notation)", "content_type": "application/json" } } } } } }

which shows that trying to access an unsupported route, gives as output, this:

an overview, supported URLs/routes, HTTP methods, and documentation about how to use it and the output formats - almost none of which code was written for, mind.

Go give your Python code a hug!

- Vasudev Ram - Online Python training and consulting on my forthcoming apps and content.Jump to posts:**Follow me on:*

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: JavaPythonJavaScriptUTFU
分页:12
转载请注明
本文标题:Vasudev Ram: Give your Python function a {web,CLI} hug!
本站链接:http://www.codesec.net/view/520159.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(51)