"Using python to do HTTP interface testing" study notes chapter 2

Chapter two

Section 1:

Write a Python program, showjson.py, run the program, it will prompt "Please enter your name:", after pressing Enter, it will prompt, "Please enter your phone number:", after pressing Enter, print out what you entered in JSON format The communication information is as follows

import json
name = input("请输入你的名字")
phone = input("请输入你的电话号码")
data = json.dumps({"name": name,
                   "phone": phone}, ensure_ascii=False, indent=4)
print(data)

Section 2 omitted

Section 3:

Write a Python program, getip.py, run the program, and print out "Your IP address is: [Network IP address of this machine]"

import requests

r = requests.get("http://httpbin.org/ip")
ip = r.json()['origin']
print("你的IP地址为:{}".format(ip))

Section 4 skipped

Section 5:

Write a Python program, getipinfo.py, run the program, and print out "Please enter the IP address you want to query: [Internet IP address of this machine]", enter the IP address, and press Enter, and return to the country where the IP address is located ( country), area (area), province (region) and city (city)

import requests
payload = {'ip': '124.128.22.31'}

r = requests.get("http://ip.taobao.com/service/getIpInfo.php", params=payload)
response = r.json()
country = response['data']['country']
city = response['data']['city']
ip = response['data']['isp']
print("IP所在国家是: {}".format(country), "\n"
      "IP所在城市是: {}".format(city), "\n"
      )

Section 6:

Write a Python program, sendpost.py, run the program, print out "Please enter your name:", enter your name, and press Enter, print out "Please enter your email address:", enter your email address, and press Enter, send The URL http://httpbin.org/post sends the data defined as follows,
data={“name”:[input name],”email”:[input email address]}
and prints the returned status code and json beautified data come out.

import requests
import json

name = input('请输入你的名字')
phone = input('你的电话')
url = 'http://httpbin.org/post'
data = {'name': name, 'phone': phone}
r = requests.post(url, data=data)
print(json.dumps(r.json(), ensure_ascii=False, indent=4))

Section 7:

Write a Python program, redirect.py, run the program, and print out "Please enter the number of redirection jumps (certificates between 1-10):", after entering the number and press Enter, the program prints out the request to the request.get method. http://httpbin.org/redirect/[input number] After sending a GET request, the status code should be 200, and the Location of each jump in the request is obtained and printed out.
The history attribute of the Response object is a list type data that contains the Response objects of each jump.

import requests

jump = input("请输入重定向调转的次数(1-10之间的整数):")
result = requests.get("http://httpbin.org/redirect/"+jump)
count = 1
for response in result.history:
    print("第{0}跳:Location={1}".format(count, response.headers["Location"]))
    count += 1

Section 8 skipped

Section 9:

The topic is too long, skip it

# -*-coding:utf-8-*-
# 原作者代码
import requests
import json
url = "http://httpbin.org/cookies"

commanddesp='''请输入cookies指令: 
add key=value ,用于增加cookies
del key       ,用于删除cookies
show          ,用于显示当前的cookies
quit          , 退出
'''
def printresult(result):
    print(json.dumps(result.json(),indent=4))

def addcookie(strcookie, session):
    print(url+"/set?"+strcookie)
    printresult(session.get(url+"/set?"+strcookie))

def deletecookie(strcookie, session):
    printresult(session.get(url+"/delete?"+strcookie))

def showcookies(session):
    printresult(session.get(url))

session=requests.session()
command=input(commanddesp)
while(command):
    if(command.split()[0]=='add'):
        addcookie(command.split()[1],session)
    elif (command.split()[0]=='del'):
        deletecookie(command.split()[1],session)
    elif(command=="show"):
        showcookies(session)
    elif(command=="quit"):
        break
    else:
        print(commanddesp)
    command=input()

Guess you like

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