# !/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import pycontrol.pycontrol as pc
import time
import csv
import ssl
from datetime import date, datetime
import urllib2
import urllib
import cookielib
import json
import httplib
import json
import datetime
import re
import requests
import StringIO
import time
import sys
import json
import re
import hashlib
import xlrd
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
else:
return json.JSONEncoder.default(self, obj)
def batch_upload(a, b, c):
s = requests.session()
cookies = requests.utils.dict_from_cookiejar(s.cookies)
print cookies
##比如要批量关闭CNAPS的所有监控
username = a
password = b
arr01=c
#
# ctime=json.dumps(arr01[5], cls=DateEncoder)
# ctime=ctime.replace('"','')
# dtime=json.dumps(arr01[9], cls=DateEncoder)
# dtime = dtime.replace('"', '')
ctime=arr01[5]
dtime=arr01[9]
print 'ctime'
print ctime
print 'ctime'
print 'dtime'
print dtime
print 'dtime'
myurl1 = 'http://20.5.101.171:8080/balantflow/restservices/addTaskApi'
#data = {'j_username': username, 'j_password': password}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',
#'Referer': 'http://20.5.101.171:8080/balantflow/',
'Content-Type': 'application/json;charset=utf-8',
'X-Requested-With': 'XMLHttpRequest'
}
data = {
"title": arr01[0],
"content": arr01[1],
##事件上报通道
"channelId": "17",
# 事件影响级别,默认8级
"urgency": "1008",
"owner": '015208',
"properties": [
{
"key": "type",
"value": arr01[3]
},
{
"key": "system",
"value": arr01[2]
},
# level系统级别
{
"key": "level",
"value": "一般"
},
{
"key": "source",
"value": arr01[4]
},
{
"key": "matrix",
"value": "系统预警"
},
{
"key": "alerttime",
"value": ctime
},
{
"key": "caseReason",
"value": arr01[6]
},
{
"key": "timeCost",
"value": arr01[7]
},
{
"key": "caseType",
"value": arr01[8]
},
{
"key": "recoverTime",
"value": dtime
}
]
}
print json.dumps(data)
# responce = s.post(myurl1, data=json.dumps(data), headers=headers, timeout=10)
# print responce.content
# print responce.status_code
def read_excel(a):
wb = xlrd.open_workbook(filename=a) # 打开文件
print wb.sheet_names() ##获取所有sheet页名字
print type(wb.sheet_names())
for x in wb.sheet_names():
print type(x)
print x.encode('utf-8')
sheet1 = wb.sheet_by_name(u'批量提交')
print sheet1.name, sheet1.ncols, sheet1.nrows
b = sheet1.nrows
list = [[] for i in range(b)]
for i in range(sheet1.nrows):
if i == 0:
pass
else:
print sheet1.row(i)
print type(sheet1.row(i))
for j in xrange(len(sheet1.row(i))):
if j == 5:
# print sheet1.row(i)[j].value
# print sheet1.row(i)[j].value
##excel日期转换,
print xlrd.xldate_as_datetime(sheet1.row(i)[j].value, 0)
#time.sleep(10)
list[i].append(xlrd.xldate_as_datetime(sheet1.row(i)[j].value, 0))
# print sheet1.row(i)[j].value.ctype
# print date(sheet1.row(i)[j].value).strftime("%Y-%m-%d %H:%M:%S")
elif j == 9:
print xlrd.xldate_as_datetime(sheet1.row(i)[j].value, 0)
# time.sleep(10)
list[i].append(xlrd.xldate_as_datetime(sheet1.row(i)[j].value, 0))
else:
#print sheet1.row(i)[j].value
list[i].append(sheet1.row(i)[j].value)
return list
##删除空数组
newlist = read_excel('test.xlsx')
while [] in newlist:
newlist.remove([])
#print newlist
for x in newlist:
print batch_upload('015208','zyj@1985724',x)
for y in x:
print y
C:Python27python.exe C:/Users/tlcb/PycharmProjects/untitled/excel/t5.py
[u'u6279u91cfu63d0u4ea4', u'u767bu8bb0']
<type 'list'>
<type 'unicode'>
批量提交
<type 'unicode'>
登记
批量提交 10 2
[text:u'u6570u636eu5e93SQLu95eeu9898u534fu52a9u67e5u770b', text:u'u6570u636eu5e93SQLu53cdu5e94u6162', text:u'u589eu503cu7a0eu7ba1u7406u7cfbu7edf', text:u'u5355u4e2au7528u6237', text:u'u5176u4ed6', xldate:43600.4930555556, text:u'u4e1au52a1u90e8u9700u8981u8c03u6574', number:60.0, text:u'u54a8u8be2u5efau8bae', xldate:43600.5347222222]
<type 'list'>
2019-05-15 11:50:00
2019-05-15 12:50:00
{}
ctime
2019-05-15 11:50:00
ctime
dtime
2019-05-15 12:50:00
dtime
Traceback (most recent call last):
File "C:/Users/tlcb/PycharmProjects/untitled/excel/t5.py", line 169, in <module>
print batch_upload('015208','zyj@1985724',x)
File "C:/Users/tlcb/PycharmProjects/untitled/excel/t5.py", line 122, in batch_upload
print json.dumps(data)
File "C:Python27libjson\__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "C:Python27libjsonencoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:Python27libjsonencoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "C:Python27libjsonencoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2019, 5, 15, 11, 50) is not JSON serializable
Process finished with exit code 1
需要进行转换:
# ctime=json.dumps(arr01[5], cls=DateEncoder)
# ctime=ctime.replace('"','')
# dtime=json.dumps(arr01[9], cls=DateEncoder)
# dtime = dtime.replace('"', '')