python学习之http服务和类

发送http请求
import requests

# 生成url
def create_header():
header = {
“content-type”: “application/json”
}
return header

body = {“name”: “zhangsan”}
headers = create_header()
print(headers)
response = requests.post(“http://localhost:8085/test/length”, json=body, headers=headers)
if response.ok:
print(response.text)
else:
print(response.reason)

提供web接口
import flask
import time

server = flask.Flask(__name__)

@server.route(‘/time’)
def test():
return str(time.time())

server.run(‘127.0.0.1’, 9000)

类的定义
class people:
def __init__(self, name, city):
self.name, self.city = name, city

def __str__(self):
return “name=%s, city=%s” % (self.name, self.city)

def moveTo(self, new_city):
self.city = new_city

def __lt__(self, other):
return self.city < other.city

__repr__ = __str__

class teacher(people):
def __init__(self, name, city, school):
self.name, self.city, self.school = name, city, school

def __lt__(self, other):
return self.school < other.school

p1 = people(“张三”, “beijing”)
print(p1)
p1.moveTo(“shanghai”)
print(p1)
people_list = list()
people_list.append(p1)
people_list.append(people(“李四”, “chongqin”))
people_list.append(people(“王五”, “nanjin”))
print(people_list)
people_list.sort()
print(people_list)

teacher_list = list()
teacher_list.append(teacher(“蔡元培”, “北京”, “beida”))
teacher_list.append(teacher(“胡适”, “青岛”, “tsinghua”))
teacher_list.append(teacher(“陈独秀”, “厦门”, “fudan”))
print(teacher_list)
teacher_list.sort()
print(teacher_list)