CMDB服务器管理系统【s5day89】:采集资产之汇报信息

1、服务器端收到的数据和客户端的数据不一样

print(request.post)

少发了,还是少取了,说明根本没有把数据全发过来

print(request.body)

 

1、只把字典的key给我发过来的

答:这是正常的,这种方式去提交的时候,伪造的是from表单的提交

2、你发数据向后台发点什么?

只可能存字符串,不可能存在字典

2、post是怎样把请求发到后台的

要发怎么发?把字典转换成字符串,必须这么发,因为后台它只认识这种请求头,根据这种结构进行解析

username: 'xxx'
username: '123'
hobby: [1,2,3,4]
hobby: {}


$ajax({
	url: 'xx',
	data:{
		k1: 1,
		k2: "xx"
		k2: [1,2,3,4],
		k2: JSON.stringfy({'kk1':'xxx'}),
	}
	
})

#Form表单提交和默认提交 + request.post(data='xxx')
#k1=1&k2=xx, content-type: annotation/x-www-form-urlencoded
后台:	request.POST,
			- annotation/x-www-form-urlencoded
			- 根据这种结构解析: k1=1&k2=xx
			
#定义请求头:
$ajax({
	url: 'xx',
	headers:{'content-type':'application/json'},
	data: JSON.stringfy({k1':1,k2:"xx",k2: [1,2,3,4],}),
})

#"{k1':1,k2:"xx",k2: [1,2,3,4],}", content-type: application/json

后台:	request.POST,
			- annotation/x-www-form-urlencoded
			- 根据这种结构解析: k1=1&k2=xx
		request.body 

具体代码

class BaseClient(object):
    def __init__(self):
        self.api = settings.API


    def post_server_info(self,server_dict):
        # requests.post(self.api,data=server_dict) # 1. k=v&k=v,   2.  content-type:   application/x-www-form-urlencoded
        response = requests.post(self.api,json=server_dict) # 1. 字典序列化;2. 带请求头 content-type:   application/json

    def exec(self):
        raise NotImplementedError('必须实现exec方法')

3、线程池

1、引子:

如果是100台每台等2分钟,ssh是串行要等200分钟

2、代码如下:

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time

def task(arg1,arg2):
    print(arg1,arg2)
    time.sleep(1)

pool = ProcessPoolExecutor(20)

for i in range(100):
    pool.submit(task,i,i)

3、关于线程池的说明:

1、刚开始我们不知道多少个池子,我就默认创建20个
2、现在他没有执行,当我们用的时候边用边执行

4、进程池

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time

def task(arg1,arg2):
    print(arg1,arg2)
    time.sleep(1)

pool = ThreadPoolExecutor(20)

for i in range(100):
    pool.submit(task,i,i)

小结:

1、python2默认没有线程池,只有进程池,python3就有

2、通过一行代码给它改成进程池

3、不要用python2以前里有进程池,很麻烦

 

posted @ 2018-07-29 12:44  活的潇洒80  阅读(249)  评论(0编辑  收藏  举报