美文网首页大数据 爬虫Python AI SqlPython小哥哥
使用Python编写API接口和使用API接口!哪个更吊?

使用Python编写API接口和使用API接口!哪个更吊?

作者: 14e61d025165 | 来源:发表于2019-04-24 16:17 被阅读26次

Get 方法实现(代码中已经注释明白)

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># coding:utf-8import jsonfrom urlparse import parse_qsfrom wsgiref.simple_server import make_server
dic_t = {"test1" :'Hello', ##此处定义一个字典 "test2" :'Hi'} ##用于返回网址中的参数对应值
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) params = parse_qs(environ['QUERY_STRING']) name = params['name'][0] ## 得到网址中的参数 try: dic = {name: dic_t[name]} ##字典查值并返回为字典 except KeyError: dic = {name: "KeyError"} ##如果字典中没有,则返回‘KeyError’ return [json.dumps(dic)] ## 网页返回值
if name == "main": port = 5088 ##自定义开启的端口 httpd = make_server("0.0.0.0", port, application) print "serving http on port {0}...".format(str(port)) httpd.serve_forever()
</pre>

程序运行后,网址中输入 http://127.0.0.1:5088/?name=test2进行访问,得到对应数据。

可以添加QQ群1004391443,有飞机大战、颜值打分器、打砖块小游戏、红包提醒神器、小姐姐表白神器等具体的实训项目,有清晰源码,有相应的文件

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1556093839021 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

Linux中一些常用操作实例

实例1:服务器后台运行任务,获取任务运行状态并且作为api的返回参数

服务器端代码实现

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># coding:utf-8import jsonfrom urlparse import parse_qsfrom wsgiref.simple_server import make_serverimport os
def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) params = parse_qs(environ['QUERY_STRING']) name = params.get('name', [''])[0] no = params.get('no', [''])[0] dic = {'name': name, 'no': no} job = os.popen("ps -a | grep ps | wc -l").readlines() job = job[0]
return job
if name == "main": port = 5088 httpd = make_server("0.0.0.0", port, application) print "serving http on port {0}...".format(str(port)) httpd.serve_forever()
</pre>

代码中的‘ps -a | grep ps | wc -l’即列出用户的所有任务,筛选任务中带有‘ps’字符的任务,返回任务的个数,可以将‘ps’改成你的任务名称。

用户端代码实现

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import osimport timedef get_job(): url = 'http://服务器IP地址:5088' job = os.popen("curl {0}".format(url)).readlines() job = job[0] return jobwhile True: try: job = get_job() except: job = 404 f = open("job_num.txt","w") f.write(job) f.close() time.sleep(60)
</pre>

如果能够得到api的返回值,则job值为真实值,如果获取失败,则job值为404.

相关文章

网友评论

    本文标题:使用Python编写API接口和使用API接口!哪个更吊?

    本文链接:https://www.haomeiwen.com/subject/urwggqtx.html