How python calls the api interface:
Interface protocol: http, webservice, rpc, etc.
Request method: get, post method
Request parameter format:
a. Get requests are all through url?param=xxx¶m1=xxx
b. Common types of request parameters for post requests include: application/json, application/x-www-form-urlencoded, multipart/form-data, text/html, etc.
You also need to know the url of the interface, parameter type, data format of the returned result, and whether the interface has header, cookie and other information.
Related recommendations: "python video tutorial"
##get method
# coding:utf-8 import json from urlparse import parse_qs from wsgiref.simple_server import make_server # 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。 def application(environ, start_response): # 定义文件请求的类型和当前请求成功的code start_response('200 OK', [('Content-Type', 'text/html')]) # environ是当前请求的所有数据,包括Header和URL,body,这里只涉及到get # 获取当前get请求的所有数据,返回是string类型 params = parse_qs(environ['QUERY_STRING']) # 获取get中key为name的值 name = params.get('name', [''])[0] no = params.get('no', [''])[0] # 组成一个数组,数组中只有一个字典 dic = {'name': name, 'no': no} 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()
Request instance
post method
# coding:utf-8 import json from wsgiref.simple_server import make_server # 定义函数,参数是函数的两个参数,都是python本身定义的,默认就行了。 def application(environ, start_response): # 定义文件请求的类型和当前请求成功的code start_response('200 OK', [('Content-Type', 'application/json')]) # environ是当前请求的所有数据,包括Header和URL,body request_body = environ["wsgi.input"].read(int(environ.get("CONTENT_LENGTH", 0))) request_body = json.loads(request_body) name = request_body["name"] no = request_body["no"] # input your method here # for instance: # 增删改查 dic = {'myNameIs': name, 'myNoIs': no} return [json.dumps(dic)] if __name__ == "__main__": port = 6088 httpd = make_server("0.0.0.0", port, application) print "serving http on port {0}...".format(str(port)) httpd.serve_forever()
Request instance
The above is the detailed content of How to call api interface in python. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
