python第六课

£可爱£侵袭症+ 提交于 2019-12-05 08:41:30

 

第六课:from urllib import requestfrom urllib import parseimport jsonimport requests------urllib-------# get请求方式url = 'http://api.nnzhp.cn/api/user/stu_info'data = {"stu_name":"xiaohei"}tmpdate = parse.urlencode(data)   #将数据变换为KV  K=vprint(tmpdate)# 接口 + 参数tmpurl = url + '?' + tmpdate    #接口和参数拼接res = request.urlopen(tmpurl)     #请求接口resForRead = res.read().decode()    #通过read方法获取返回值结果,返回值结果是bytes;通过decode将bytes转换成字符串resFORDict = json.loads(resForRead)   #通过json将字符串转换为字典print(resForRead)print(resFORDict)# post请求方式url = "http://api.nnzhp.cn/api/user/login"date = {"username":"niuhanyang","passwd":"aA123456"}tmpdate = parse.urlencode(date)          #将数据变换为KV  K=v# 接口+参数res = request.urlopen(url,tmpdate.encode())     #post请求,参数一url,参数二接口地址,参数二参数要求bytesprint(res.read().decode())requesrsget方法url = 'http://api.nnzhp.cn/api/user/stu_info'data = {"stu_name":"xiaohei"}res = requests.get(url,data).text      #test方法返回的是字符串格式格式的返回值res = requests.get(url,data).json()       #json()方法返回的是字典格式格式的返回值print(res)post方法url = "http://api.nnzhp.cn/api/user/login"date = {"username":"niuhanyang","passwd":"aA123456"}res = requests.post(url,date).text      #test方法返回的是字符串格式格式的返回值res = requests.post(url,date).json()       #json()方法返回的是字典格式格式的返回值print(res)# 入参是json的url = 'http://api.nnzhp.cn/api/user/add_stu'data = {"name":"dsx123","grade":"一班","phone":11099999991}res = requests.post(url,json=data).json()    #接口要求入参是json类型,可以通过在post请中指定jsonprint(res)cookiecookie = {"niuhanyang":"0d11299888c03b25c9f89bb1231de23d"}url = 'http://api.nnzhp.cn/api/user/gold_add'date = {"stu_id":1,"gold":10000}res = requests.post(url,date,cookies = cookie).test   #通过cookies进行cookies的传递print(res)headerurl = "http://api.nnzhp.cn/api/user/all_stu"header = {"Referer":"http://api.nnzhp.cn/"}res = resquests.get(url,headers = header).textprint(res)# 传文件到服务器上url = "http://api.nnzhp.cn/api/file/file_upload"# 通过files参数将文件传递到服务器上res = requests.post(url,files={"file":open('__init__.py','rb')}).text       #open操作打开文件名即可print(res)习题:提供一个方法,获取到登录后的signdef getSign():    url = "http://api.nnzhp.cn/api/user/login"    date = {"username":"niuhanyang","passwd":"aA123456"}    # res = requests.post(url,date).text      #test方法返回的是字符串格式格式的返回值    res = requests.post(url,date).json()       #json()方法返回的是字典格式格式的返回值    sign = res.get('login_info').get('sign')    print(sign)getSign()jsonpath  #字典里面取值      返回listlog = nnlog.Logger('book_server.log',backCount=5,level='DEBUG',when='D')#实例化,level默认级别,backCount保留几个日志,最近几个,def getSign():    url = "http://api.nnzhp.cn/api/user/login"    date = {"username":"niuhanyang","passwd":"aA123456"}    log.debug(date)    # res = requests.post(url,date).text      #test方法返回的是字符串格式格式的返回值    res = requests.post(url,date).json()       #json()方法返回的是字典格式格式的返回值    sign = jsonpath.jsonpath(res,'$..sign')   #需要通过$..来定位     $代表字典(搜索范围)   ..代表模糊查询    sign = jsonpath.jsonpath(res,'$.sign')   #需要通过$..来定位     $代表字典   ..代表模糊查询    log.info(sign)getSign()import nnloglog = nnlog.logger('book_server.log',backCount=5,lever='debug')#参数一,log的路径(可以是不存在的文件会自动创建文件)   #参数二,保存最近5天的日志log.debug('XXX值是什么')    #测试程序的时候使用;打印时间,当前是那个一个文件在运行,哪行代码在运行log.info('调用了什么')     #上线为了节省磁盘空间,可以考虑少打debuglog.warning('警告')log.error('出错')发送邮件# 163邮箱username='18792994577@163.com' # 发件人的邮箱password='7788nn'#163、126发件人的密码# QQ邮箱  开启imapusername= '1871042016@qq.com'password = 'bxawncbwacpacfba'    #qq生成授权码mail_server = 'smtp.163.com'   #163的服务mail_server = 'smtp.qq.com'      #QQ的服务mail_server = 'smtp.126.com'      #126的服务m = yagmail.SMTP(user=username,password=password,host=mail_server)    #实例化,用哪个账户密码那个服务发送给谁to = ['18792994577@163.com']    #发送给谁cc = ['18792994577@163.com']     #抄送给谁m.send(to=to,cc=cc,       subject='是否正常发送',   #邮件标题       contents='能正常发送',    #邮件正文       attachments=r'1.txt')     #邮件附件# moke接口开发# 1、模拟第三方接口# 2、给别人提供数据# 3、flask是web开发框架import flask,jsonserver = flask.Flask(__name__)    #吧python文件当中服务@server.route('/api/login')def login():    username=flask.request.values.get('username')   #从请求中获取参数    flask.request.json.get()  #入参是json的话,用这个    d = {'error_code':1,'msg':'登录成功'}    return json.dumps(d,ensure_ascii=False)server.run(host='127.0.0.1',port=8000,debug=True)openpyxl模块-文件读写# 写excelbook = openpyxl.Workbook()sheet = book.active      #默认带的sheet页sheet2 = book.get_sheet_by_name('sheet2')     #添加sheet页sheet.append(['id','username','password'])     #添加整行sheet.append(['01','zhangsan','123456'])     #添加整行sheet.append(['02','lisi','147258'])     #添加整行sheet['a4']='03'sheet['b4']='zhaoliu'sheet.cell(5,2,'leqi')book.save('students.xlsx')# 读excelbook = openpyxl.load_workbook('students.xlsx')sheet = book.activesheet = book.get_sheet_by_name()print(sheet.cell(1,1).value)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!