python学习小记
python HTTP请求示例:
# coding=utf-8 # more materials: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html import requests
import json
import time import pymysql myhost = "http://127.0.0.1:8080"
myurl = ""
mytoken = ""
myheaders = {}
myparams = {}
mybodydata = {} def test1():
# POST demo
print "\n POST demo"
myurl = myhost + "/api/v1/auth/login"
mybodydata = {"username": "杜甫", "password": ""}
r = requests.post(myurl, data=json.dumps(mybodydata))
# print r.text
mytoken = r.json()["token"]
print mytoken # GET demo
print "\n GET demo"
myurl = myhost + "/api/v1/student/experiment"
myheaders = {"X-Authorization": "Bearer "+mytoken}
myparams = {
"studentId": "664e8106-0552-45e9-8518-5fab535fe036", "chapter": 2}
r = requests.get(myurl, headers=myheaders, params=myparams)
# print r.text
print r.json()[0]["studentId"] # PUT demo
print "\n PUT demo"
myurl = myhost + "/api/v1/student/experiment/completion"
myheaders = {"X-Authorization": "Bearer "+mytoken}
myparams = {"studentId": "664e8106-0552-45e9-8518-5fab535fe036", "experimentId": "2.1",
"modifyTimestamp": "2018-11-21 11:10:11", "finishedSteps": 3}
r = requests.put(myurl, headers=myheaders, params=myparams)
# print r.text
# print r.json()["studentId"]
return
# test1() def test2(i, postfix):
# POST demo
print "\n POST demo"
myurl = myhost + "/api/v1/admin/student"
myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
mybodydata = {"schoolId": "", "schoolNumber": "SY1506"+postfix,
"name": name[i], "gender": "", "grade": "高一(6)班",
"age": 18, "status": "online", "bz": bz[i],
"phone": "", "ip": ip[i], "port": port[i]}
r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata))
print r.text
# mytoken=r.json()["token"]
# print mytoken
return
# 不能有分号,fuck
# test2() 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",
"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"]
port = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
name = ["林达华", "戴娟", "周丹", "倪枫", "范青", "郑文滨", "张富华", "蔡文晖", "吴军", "王若晖", "李治中", "朱雁博", "李晓波", "吴青", "张绍铭", "杨焕州", "王睿", "陈向东", "严一滨", "周畅", "胡志洪", "朱元锟", "敖培", "冷春波", "马军", "张龙", "王英", "段斯译", "闫兴华", "黄山", "方小培", "汪义超", "王梦", "佟彤", "张秀政", "李卫平", "曾皓明",
"王广科", "张振堂", "徐正一", "沈杨", "何博", "刘晓丹", "钱 晋", "王霄驰", "穆青", "朱永生", "崔懿", "朱勋", "万景华", "王美佳", "王辉", "江艳萍", "张炜其", "李守志", "刘平", "李鲋瑞", "王智荣", "于新平", "常学勤", "王明江", "章良", "姜波", "王建伟", "武绍玮", "吴时敏", "蓝滚波", "stu01", "stu02", "stu03", "stu04", "stu05"]
bz = ["商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "商汤", "华东师范大学第二附属中学", "华东师范大学第二附属中学", "华东师范大学第二附属中学乐东黄流中学", "上海交通大学附属中学", "上海交通大学附属中学", "上海市晋元高级中学", "上海市晋元高级中学", "中山纪念中学", "晋城市第一中学", "山西省汾阳中学校", "深圳外国语学校", "广东广雅中学", "广东广雅中学", "合肥市第一中学", "合肥市第一中学", "中央民族大学附属中学", "哈尔滨工业大学附属中学校", "山西孝义中学", "孝义市教育局", "浙江省镇海中学", "江苏省邗江中学", "甘肃省兰州第一中学",
"上海市格致中学", "上海市格致中学", "上海市格致中学", "上海市格致中学", "上海市市西中学", "上海市市西中学", "山东省青岛第二中学", "合肥市第六中学", "上海市七宝中学", "河南大学附属中学", "东北育才学校", "辽宁省实验中学", "新疆农业大学附属中学", "浙江省温岭中学", "北京市第十一中学校", "贵阳市第一中学", "深圳华侨城中学", "深圳华侨城中学", "山西省实验中学", "山西省实验中学", "未来科技", "西北师范大学附属中学", "上海光华教育集团", "郑州市第二中学", "郑州市第二中学", "郑州市第二中学", "贵阳一中金塔英才学校", "博罗县东江广雅学校", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号", "绑定端口的学生测试账号"] for i in range(1, 73):
num = "%02d" % i
# test2(i-1,num) for i in range (24,25):
num = "%03d" % i
myhost="http://10.5.31.15:8081"
myurl = myhost + "/api/v1/admin/student"
myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
mybodydata = {"schoolId": "", "schoolNumber": "cmcc"+num,
"name": "邢孝慈", "gender": "", "grade": "高一(6)班",
"age": 18, "status": "online", "bz": "student test account",
"phone": "", "ip": "10.5.31.15", "port": 5000+i}
# print mybodydata
# r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata))
# print r.text def test3():
print "\n PUT demo"
myurl = myhost + "/api/v1/teacher/course/experiment/step"
myheaders = {"X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLlvKDogIHluIgiLCJzY29wZXMiOlsiUk9MRV9URUFDSEVSIl0sInVzZXJJZCI6InRlYWNoZXIwMDAxIiwiaXNzIjoiemh1eWFuYm8iLCJpYXQiOjE1MjgxMDExODcsImV4cCI6MTUzMTcwMTE4N30.7c7j4vCqqNoguoA6FXuTXqkIXPLdYrSf9kYl-Yurmi0gjrOwukt5eDM1bW1O_L4yz6gbo_FpPH1murvFg8-EUg"}
myparams = {"courseId": "", "experimentId": "2.1",
"step": 1}
mybodydata = "\"he<p>你好aolisfjdlasfjklis</p>he\""
print mybodydata
r = requests.put(myurl, headers=myheaders,
params=myparams, data=mybodydata)
print r.text
# test3() # 实验步骤插入
myurl = myhost + "/api/v1/teacher/course/experiment/step"
myheaders = {'content-type': 'application/json', "X-Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLnrqHnkIblkZgiLCJzY29wZXMiOlsiUk9MRV9BRE1JTiJdLCJ1c2VySWQiOiJhZG1pbjAwMDEiLCJpc3MiOiJ6aHV5YW5ibyIsImlhdCI6MTUyODMzODkzMCwiZXhwIjoxNTMxOTM4OTMwfQ.psAqK4nl5eE9DkD14c3TMYXaMkp4ydDnjNagT61ja96WzBl41M3ZTL-oatMXSqvXk1ExDQkW0SCyl01HJyEiSg"}
for i in range (1,7):
tmpContent= "(请添加 实验 0-4 步骤 "+ str(i) +" 的内容描述)"
mybodydata = {"experimentId": "0.4", "content": tmpContent }
# r = requests.post(myurl, headers=myheaders, data=json.dumps(mybodydata)) def sqlTest():
try:
conn = pymysql.connect(host="127.0.0.1", db="sensestudydev", user="root", passwd="", port=3306)
sql = "select * from user"
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
print rows[0]
# print json.dumps(json.loads(rows[0][0]), indent=4)
except:
conn.close()
conn.close()
sqlTest()
python学习小记的更多相关文章
- Python学习小记(5)---Magic Method
具体见The Python Language Reference 与Attribute相关的有 __get__ __set__ __getattribute__ __getattr__ __setat ...
- Python学习小记(4)---class
1.名称修改机制 大概是会对形如 __parm 的成员修改为 _classname__spam 9.6. Private Variables “Private” instance variables ...
- Python学习小记(3)---scope&namespace
首先,函数里面是可以访问外部变量的 #scope.py def scope_test(): spam = 'scope_test spam' def inner_scope_test(): spam ...
- Python学习小记(1)---import小记
在这种目录结构下,import fibo会实际导入fibo文件夹这个module λ tree /F 卷 Programs 的文件夹 PATH 列表 卷序列号为 BC56-3256 D:. │ fib ...
- Python学习小记(2)---[list, iterator, and, or, zip, dict.keys]
1.List行为 可以用 alist[:] 相当于 alist.copy() ,可以创建一个 alist 的 shallo copy,但是直接对 alist[:] 操作却会直接操作 alist 对象 ...
- python 学习小记之冒泡排序
lst =[11,22,44,2,1,5,7,8,3] for i in range(len(lst)): i = 0 while i < len(lst)-1: ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- Python学习--01入门
Python学习--01入门 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.和PHP一样,它是后端开发语言. 如果有C语言.PHP语言.JAVA语言等其中一种语言的基础,学习Py ...
随机推荐
- 最近公共祖先 LCA 倍增算法
树上倍增求LCA LCA指的是最近公共祖先(Least Common Ancestors),如下图所示: 4和5的LCA就是2 那怎么求呢?最粗暴的方法就是先dfs一次,处理出每个点的深度 ...
- Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决
按照官方的文档写的demo,只是多了个init函数,最终执行时提示没有_rules这个属性的错误日志如下: ...... File "C:\ProgramData\Anaconda3\lib ...
- PHP判断iPhone、iPad、Android、PC设备的方法
因为工作需要我们需要知道是什么样了用户访问了我网站了,现在的移动设备种类多了,下面我们一起来看小编整理的一段php判断iPhone.iPad.Android.PC设备的例子. 注意:本代码的PC系统为 ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习1
#include <iostream>using namespace std;int main(){ int min,max; cout<<"Enter the mi ...
- springmvc注解方式
https://www.cnblogs.com/shanheyongmu/p/5865589.html
- Oracle中 (+)与left join 的用法区别
Oracle中 (+)与left join 的用法区别 原创 2017年01月11日 13:33:42 6648 select * from a,b where a.id=b.id(+); (+)写在 ...
- sql 索引笔记--索引组织结构
非聚集索引与聚集索引具有相同的 B 树结构,它们之间的显著差别在于以下两点: 基础表的数据行不按非聚集键的顺序排序和存储. 非聚集索引的叶层是由索引页而不是由数据页组成. 既可以使用聚集索引来为表或视 ...
- JAVA自学笔记19
JAVA自学笔记19 1.集合总结 Collection(单列集合) List(有序可重复) ArrayList:底层数据结构是数组 ,查询快,增删慢.线程不安全,效率高 Vector:底层数据结构是 ...
- 7、js对象
在python中我们学习了面向对象,javascript也是一门面向对象语言,在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象. 本篇导航: String对象 ...
- C# CancellationTokenSource和CancellationToken的实现
微软关于CancellationTokenSource的介绍很简单,其实CancellationTokenSource的使用也很简单,但是实现就不是那么简单了,我们首先来看看CancellationT ...