Using Python for API interface testing

Source: Internet
Author: User

This text connection: http://blog.csdn.net/freewebsys/article/details/46228421 reprint Please specify the source!

1, about requests

Requests is a Python HTTP client library that is designed to be very simple and specifically written for simplified HTTP testing.

Website:
https://pypi.python.org/pypi/requests/
http://docs.python-requests.org/en/latest/user/quickstart/#make-a-request
GitHub Project Address
https://github.com/kennethreitz/requests/

2, development environment

It is very convenient to build a development environment under Mac.

sudo easy_install pipsudo pip install requests

Under test: Python command line

ImportRequests>>>R = Requests.get (' Https://api.github.com/user ', Auth= (' user ',' Pass '))>>>R.status_code $>>>r.headers[' Content-type ']' Application/json; Charset=utf8 '>>>R.encoding' Utf-8 '>>>R.textu ' {' type ': ' User ' ... '>>>R.json () {u ' private_gists ':419,u ' total_private_repos ': the, ...}

development tool, previously using sublime, found run error, do not recognize table character.

notany outer indentation level

Very crazy mistake, there is no code where there is a problem. Even began to doubt the life.
Python's space-coded code is really crazy. Begin to miss the language with curly braces, semicolons.

A complete solution, switch directly to the IDE tool. Using Sledgehammer, IDA develops.
: https://www.jetbrains.com/pycharm/download/
Download the community version directly, because it's written in a script and doesn't use a very complex framework.

Really good effect, direct format code, and Java as well, can run can debug. Right-click directly to run successfully.

3, Test interface

Nothing is too complicated, just use the requests framework.

#!/usr/bin/python# -*- coding: utf-8 -*-################# python简明教程 http://woodpecker.org.cn/abyteofpython_cn/chinese/ import requests#测试百度def baidu_func(url):    headers = {}    params = {}    req = requests.post(url, headers=headers, params=params)    print(req.text)if‘__main__‘:    "http://www.baidu.com"    baidu_func(url)
4, summary

This text connection: http://blog.csdn.net/freewebsys/article/details/46228421 reprint Please specify the source!

Testing is important, especially for external interfaces, and it takes time to carefully test and analyze the code carefully.
Safety is a very important thing, take time to ponder.
Python learning is also very easy to learn, one hours to learn the grammar.
A concise tutorial for Python:
http://woodpecker.org.cn/abyteofpython_cn/chinese/
At the same time penetration testing, a lot of security scanning tools are written in Python. The Pycharm CE version of the development tool is powerful enough to help you learn python quickly.
Wxpython is a great choice if you want to quickly develop a point-and-click interface.

Using Python for API interface testing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.