相关文章推荐
帅呆的生菜  ·  python socket.error: ...·  1 年前    · 
想发财的茴香  ·  asp.net ...·  2 年前    · 

因为mongoDDB的账号和密码是针对单独数据库使用的,所以在进行连接指定数据库时候,需要使用当前库的账号和密码

如果不用指定数据库,需要使用root账号密码连接,不需要authSource参数


class MogoContext():
def __init__(self,rw='r',db='cmdb2') -> None:
user='cmdb_user'
pwd='1q2w3e'
host='host'
# self.uri="mongodb://%s:%s@%s" % (quote_plus(user), quote_plus(pwd), host)
# db 指定要连接的数据库,
# user/pwd指定数据库的用户密码
self.client = MongoClient(host=host, port=27017, username=user, password=pwd,authSource=db)
# conn = MongoClient('mongodb://{}:{}@{}:{}/?authSource={}'.format(user,pwd,host,27017,db))
self.db=self.client[db]

def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.client.close()


if __name__=='__main__':
with MogoContext('r','cmdb2') as mongo:
stu=mongo.db['student'].find_one({"Name":"ab"})
print(str(stu))

Python输出内容居中 python输出中间字符串

a = 'SUNW ukong 123456' #print(a.capitalize()) #字符串首字母变成大写 #print(a.casefold()) #将字符串中的大写字母全部变成小写字母 #print(a.center(50,'-')) #将字符串居中显示,总共50个字符,如果字符串的长度不够50个字符,就在字符串两边补齐‘-

背景:想给公司的进件流程写一套进件脚本,首先遇到的就是加密解密。公司用的 DES3 + base64 加密解密 一、安装  pycrypto模块,推荐用pycrypto编译文件,直接下载安装就行 http://www.voidspace.org.uk/python/modules.shtml#pycrypto 二、上代码,。此套代码是