• python学习小记


    python HTTP请求示例:

      1 # coding=utf-8
      2 
      3 # more materials: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
      4 
      5 import requests
      6 import json
      7 import time
      8 
      9 import pymysql
     10 
     11 myhost = "http://127.0.0.1:8080"
     12 myurl = ""
     13 mytoken = ""
     14 myheaders = {}
     15 myparams = {}
     16 mybodydata = {}
     17 
     18 
     19 
     20 def test1():
     21     # POST demo
     22     print "
     POST demo"
     23     myurl = myhost + "/api/v1/auth/login"
     24     mybodydata = {"username": "杜甫", "password": "123"}
     25     r = requests.post(myurl, data=json.dumps(mybodydata))
     26     # print r.text
     27     mytoken = r.json()["token"]
     28     print mytoken
     29 
     30     # GET demo
     31     print "
     GET demo"
     32     myurl = myhost + "/api/v1/student/experiment"
     33     myheaders = {"X-Authorization": "Bearer "+mytoken}
     34     myparams = {
     35         "studentId": "664e8106-0552-45e9-8518-5fab535fe036", "chapter": 2}
     36     r = requests.get(myurl, headers=myheaders, params=myparams)
     37     # print r.text
     38     print r.json()[0]["studentId"]
     39 
     40     # PUT demo
     41     print "
     PUT demo"
     42     myurl = myhost + "/api/v1/student/experiment/completion"
     43     myheaders = {"X-Authorization": "Bearer "+mytoken}
     44     myparams = {"studentId": "664e8106-0552-45e9-8518-5fab535fe036", "experimentId": "2.1",
     45                 "modifyTimestamp": "2018-11-21 11:10:11",  "finishedSteps": 3}
     46     r = requests.put(myurl, headers=myheaders, params=myparams)
     47     # print r.text
     48     # print r.json()["studentId"]
     49     return
     50 # test1()
     51 
     52 
     53 def test2(i, postfix):
     54     # POST demo
     55     print "
     POST demo"
     56     myurl = myhost + "/api/v1/admin/student"
     57     myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
     58     mybodydata = {"schoolId": "1", "schoolNumber": "SY1506"+postfix,
     59                   "name": name[i], "gender": "0", "grade": "高一(6)班",
     60                   "age": 18, "status": "online", "bz": bz[i],
     61                   "phone": "110", "ip": ip[i], "port": port[i]}
     62     r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata))
     63     print r.text
     64     # mytoken=r.json()["token"]
     65     # print mytoken
     66     return
     67 # 不能有分号,fuck
     68 # test2()
     69 
     70 
     71 ip = ["10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.18", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.21", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.24", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.27", "10.5.31.30", "10.5.31.30", "10.5.31.30", "10.5.31.30",
     72       "10.5.31.30", "10.5.31.30", "10.5.31.30", "10.5.31.30", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.33", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.36", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.39", "10.5.31.42", "10.5.31.42", "10.5.31.42", "10.5.31.42", "10.5.31.42", "10.5.31.42", "10.5.31.42", "10.5.31.42"]
     73 port = ["5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004",
     74         "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008", "5001", "5002", "5003", "5004", "5005", "5006", "5007", "5008"]
     75 name = ["林达华", "戴娟", "周丹", "倪枫", "范青", "郑文滨", "张富华", "蔡文晖", "吴军", "王若晖", "李治中", "朱雁博", "李晓波", "吴青", "张绍铭", "杨焕州", "王睿", "陈向东", "严一滨", "周畅", "胡志洪", "朱元锟", "敖培", "冷春波", "马军", "张龙", "王英", "段斯译", "闫兴华", "黄山", "方小培", "汪义超", "王梦", "佟彤", "张秀政", "李卫平", "曾皓明",
     76         "王广科", "张振堂", "徐正一", "沈杨", "何博", "刘晓丹", "钱 晋", "王霄驰", "穆青", "朱永生", "崔懿", "朱勋", "万景华", "王美佳", "王辉", "江艳萍", "张炜其", "李守志", "刘平", "李鲋瑞", "王智荣", "于新平", "常学勤", "王明江", "章良", "姜波", "王建伟", "武绍玮", "吴时敏", "蓝滚波", "stu01", "stu02", "stu03", "stu04", "stu05"]
     77 bz = ["商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "华东师范大学第二附属中学", "华东师范大学第二附属中学", "华东师范大学第二附属中学乐东黄流中学", "上海交通大学附属中学", "上海交通大学附属中学", "上海市晋元高级中学", "上海市晋元高级中学", "中山纪念中学", "晋城市第一中学", "山西省汾阳中学校", "深圳外国语学校", "广东广雅中学", "广东广雅中学", "合肥市第一中学", "合肥市第一中学", "中央民族大学附属中学", "哈尔滨工业大学附属中学校", "山西孝义中学", "孝义市教育局", "浙江省镇海中学", "江苏省邗江中学", "甘肃省兰州第一中学",
     78       "上海市格致中学", "上海市格致中学", "上海市格致中学", "上海市格致中学", "上海市市西中学", "上海市市西中学", "山东省青岛第二中学", "合肥市第六中学", "上海市七宝中学", "河南大学附属中学", "东北育才学校", "辽宁省实验中学", "新疆农业大学附属中学", "浙江省温岭中学", "北京市第十一中学校", "贵阳市第一中学", "深圳华侨城中学", "深圳华侨城中学", "山西省实验中学", "山西省实验中学", "未来科技", "西北师范大学附属中学", "上海光华教育集团", "郑州市第二中学", "郑州市第二中学", "郑州市第二中学", "贵阳一中金塔英才学校", "博罗县东江广雅学校", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号"]
     79 
     80 for i in range(1, 73):
     81     num = "%02d" % i
     82     # test2(i-1,num)
     83 
     84 
     85 for i in range (24,25):
     86     num = "%03d" % i
     87     myhost="http://10.5.31.15:8081"
     88     myurl = myhost + "/api/v1/admin/student"
     89     myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
     90     mybodydata = {"schoolId": "1", "schoolNumber": "cmcc"+num,
     91                   "name": "邢孝慈", "gender": "0", "grade": "高一(6)班",
     92                   "age": 18, "status": "online", "bz": "student test account",
     93                   "phone": "110", "ip": "10.5.31.15", "port": 5000+i}
     94     # print mybodydata
     95     # r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata))
     96     # print r.text
     97 
     98 
     99 def test3():
    100     print "
     PUT demo"
    101     myurl = myhost + "/api/v1/teacher/course/experiment/step"
    102     myheaders = {"X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLlvKDogIHluIgiLCJzY29wZXMiOlsiUk9MRV9URUFDSEVSIl0sInVzZXJJZCI6InRlYWNoZXIwMDAxIiwiaXNzIjoiemh1eWFuYm8iLCJpYXQiOjE1MjgxMDExODcsImV4cCI6MTUzMTcwMTE4N30.7c7j4vCqqNoguoA6FXuTXqkIXPLdYrSf9kYl-Yurmi0gjrOwukt5eDM1bW1O_L4yz6gbo_FpPH1murvFg8-EUg"}
    103     myparams = {"courseId": "1", "experimentId": "2.1",
    104                 "step": 1}
    105     mybodydata = ""he<p>你好aolisfjdlasfjklis</p>he""
    106     print mybodydata
    107     r = requests.put(myurl, headers=myheaders,
    108                      params=myparams, data=mybodydata)
    109     print r.text
    110 # test3()
    111 
    112 
    113 # 实验步骤插入
    114 myurl = myhost + "/api/v1/teacher/course/experiment/step"
    115 myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
    116 for i in range (1,7):
    117     tmpContent= "(请添加 实验 0-4 步骤 "+ str(i) +" 的内容描述)"
    118     mybodydata = {"experimentId": "0.4", "content": tmpContent }
    119     # r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata))
    120 
    121 
    122 def sqlTest():
    123     try:
    124         conn = pymysql.connect(host="127.0.0.1", db="sensestudydev", user="root", passwd="123", port=3306)
    125         sql = "select * from user"
    126         cur = conn.cursor()
    127         cur.execute(sql)
    128         rows =  cur.fetchall()
    129         print rows[0]
    130         # print json.dumps(json.loads(rows[0][0]), indent=4)
    131     except:
    132         conn.close()
    133     conn.close()
    134 sqlTest()
    View Code
  • 相关阅读:
    返回一个随机数组中的子数组中的数相加最大的和
    四则运算二之结果
    四则运算二
    UVA 11741 Ignore the Blocks
    UVA 1408 Flight Control
    UVA 10572 Black & White
    CF1138D(545,div2) Camp Schedule
    UVA 1214 Manhattan Wiring
    UVA 11270 Tiling Dominoes
    BZOJ 3261 最大异或和
  • 原文地址:https://www.cnblogs.com/z-sm/p/9364151.html
Copyright © 2020-2023  润新知