python learning network programming (file uploading and downloading) - redis operating ---- Interface Development

Requests Import 
# urllib based packaging
# URL = 'http://aliuwmp3.changba.com/userdata/userwork/1128447787.mp3'
# D = { 'stu_name': 'mineral'}
# = R & lt requests.get (URL) # get request
# print ( 'json', r.json ()) # returns a dictionary
# print ( 'text', r.text ) # returns a string
# print ( 'content', r.content ) # returns a bytes binary result is mainly used to download
# FW = Open ( 'ldh.mp3', 'WB')
# fw.write (r.content)
# fw.close ()

#post request
# url = 'http: // api . nnzhp.cn/api/user/login '
# {Data =' username ':' niuhanyang ',' the passwd ':' aA123456 '}
# = R & lt requests.post (URL, Data)
# Print (r.text)

# file Upload
# http://q4.qlogo.cn/g?b=qq&nk=921375025&s = 140 acquires qq picture
#http: //api.nnzhp.cn/api/file/file_upload Upload path
# url = 'http://api.nnzhp.cn/api/file/file_upload'
# r = requests.post(url,files={'file':open('ldh.mp3','rb')})
# print(r.text)

# r = requests.session()
# # r.get()
# result = r.post('http://api.nnzhp.cn/api/user/login',data={'username':'niuhanyang','passwd':'aA123456'})
# result = r.post('http://api.nnzhp.cn/api/user/login',params={'username':'niuhanyang','passwd':'aA123456'})

# print(result.text)



redis操作

# Redis 10w has a memory which supports a second high performance read and write data database 
Import Redis
R & lt redis.Redis = (Host = '118.24.3.40', password = '* & HK139bc', DB = 0, decode_responses = True)
# r.set ( 'user', 'white') new
# print (r.get ( 'name' ). decode ()) query


# string data type
# r.delete ( 'key') # delete
# r. set ( 'session', 'jkjkjkljkl ', 60) # set time lapse session
# r.flushdb () # redis database and clears the current key
# r.flushall () clear all key # all databases
# r.keys () # Get the current database which all key
# r.keys ( '* xx') of the key start with an overview of all of xx
# r.exists ( 'key') # determines whether or not there exists the key does not exist return 0 return 1

#hash type data
# r.hset ( 'cnz_l', 'LDH', '12345')
# r.hset ( 'cnz_l', 'LDH2', '12345') inserted into the modified hash type data #
# print (r.hget ( 'cnz_l', 'ldh ')) # get hash data
# r.hdel ( 'cnz_l', ' ldh') # remove a key hash of
# R.hgetall ( 'cnz_l') # Get all the key and value hash
# r.hmset ( 'cnz_l', { 'key': 'val', 'key1': 'val1'}) # Bulk data set
# r.type ( 'key') # View data types

redis migration
import redis

a = redis.Redis(host='118.24.3.40',password='HK139bc&*',
db=15,decode_responses=True) #0-16
b = redis.Redis(host='118.24.3.40',password='HK139bc&*',
db=10,decode_responses=True) #0-16

for k in a.keys():
if a.type(k) == 'string':
value = a.get(k)
b.set(k,value)
elif a.type(k) == 'hash':
all_data = a.hgetall(k)
b.hmset(k,all_data)
else:
print('其他类型不支持!')

接口开发

import flask
import json

#1、mock接口
#2、给别人提供数据

#flask web开发框架

server = flask.Flask(__name__)

import pymysql

def op_mysql(sql,many_tag=False):
conn = pymysql.connect(host='118.24.3.40',user='jxz',password='123456',
db='jxz',port=3306,charset='utf8',
autocommit=True)
cur = conn.cursor(pymysql.cursors.DictCursor)
cur.execute(sql)
if many_tag:
result = cur.fetchall() # [{"id":1,"name":"xxx"},{"id":2,"name":"xxx"}]
else:
result = cur.fetchone()
cur.close()
conn.close()
return result


@server.route('/index')
def login():
d = {"code":0,"msg":"登录成功 niuhanyangq111!"}
return json.dumps(d,ensure_ascii=False)


@server.route('/get_data')
def table_data():
table_list = ['app_myuser','app_product','app_student']
table_name = flask.request.args.get('table_name')
limit = flask.request.args.get('limit',10)
if table_name:
if table_name in table_list:
sql='select * from %s limit %s;'%(table_name,limit)
print(sql)
data = op_mysql(sql,True)
else:
data = {"code": -2, 'msg': "你没有权限查看这个表里面的数据!"}

else:
Required parameter is not filled, see interface document"}data = { "code": -

return json.dumps(data,ensure_ascii=False)


@server.route('/post_data',methods=['post'])
def post():
username = flask.request.args.get('username')#参数在url里面的话,用它
username2 = flask.request.values.get('username2')#参数在body里面,用它
file = flask.request.files.get('f')#上传文件
cookies = flask.request.cookies.get('f')#cookie
headers = flask.request.headers.get('f')#headers
# headers = flask.request.json.get('username')#json格式的
file.save(file.filename)
return 'abc'




server.run(host='0.0.0.0',port=8989,debug=True)




#app_user,app_product,app_student

Guess you like

Origin www.cnblogs.com/huaixiaohai/p/11129499.html