Simple case of python calling airline's interface to get air ticket data api

First of all, let’s briefly talk about the interface test. The two commonly used interfaces are the interfaces of http api and rpc protocol. Today, we mainly talk about: http api interface is a method to distinguish calls through the path of the http protocol, and the format of the request message is key-value form, the returned message is generally a json string;

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 are: application/json, application/x-www-form-urlencoded, multipart/form-data, text/html, etc.

You also need to know the url of the interface, the parameter type, the data format of the returned result, and whether the interface has headers, cookies, and other information.


The following is an example of an airline's data interface call, the username is fictitious for confidentiality:

import requests
import hashlib


def Test():
    url = 'http://api.tripsky.com.cn/ctrip/API/ctripPolicy.php'
    data = {
        'username': "KKKKKKK",
        'password': "XXXXXXX",
        'startDate': "2018-01-01",
        'endDate': "2018-01-02",
        'depcity': "FOC",
        'arrcity': "CTU",
        'code2': "MF",
    }

    # get sign
    sign = ''
    for i in data.values():
        sign += i

    # use md5 encryption
    sign = hashlib.md5(sign.encode('utf-8')).hexdigest()
    print(sign)

    # add sign to the data dictionary
    data['sign'] = sign
    print(data)

    # send post request
    response = requests.post(url, data=data).content
    print(response)
    return response

Test()

The following is the returned json data, through online parsing screenshots


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325590434&siteId=291194637
Recommended