compatible Python2.7-Python3.7

This commit is contained in:
macos
2019-12-24 18:16:08 +08:00
parent 3db1b8a243
commit ce4def5c87
2 changed files with 42 additions and 16 deletions

View File

@@ -1,9 +1,9 @@
#!/usr/bin/env python
# coding: utf-8
# Update by : https://github.com/cppla/ServerStatus
# 支持Python版本2.7 to 3.5
# 支持Python版本2.7 to 3.7
# 支持操作系统: Linux, OSX, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures
# 时间: 20190128
# 时间: 20191224
# 说明: 默认情况下修改server和user就可以了。
SERVER = "127.0.0.1"
@@ -248,6 +248,19 @@ def get_packetLostRate():
t2.start()
t3.start()
def byte_str(object):
'''
bytes to str, str to bytes
:param object:
:return:
'''
if isinstance(object, str):
return object.encode(encoding="utf-8")
elif isinstance(object, bytes):
return bytes.decode(object)
else:
print(type(object))
if __name__ == '__main__':
for argc in sys.argv:
if 'SERVER' in argc:
@@ -267,10 +280,10 @@ if __name__ == '__main__':
print("Connecting...")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((SERVER, PORT))
data = s.recv(1024)
data = byte_str(s.recv(1024))
if data.find("Authentication required") > -1:
s.send(USER + ':' + PASSWORD + '\n')
data = s.recv(1024)
s.send(byte_str(USER + ':' + PASSWORD + '\n'))
data = byte_str(s.recv(1024))
if data.find("Authentication successful") < 0:
print(data)
raise socket.error
@@ -279,7 +292,7 @@ if __name__ == '__main__':
raise socket.error
print(data)
data = s.recv(1024)
data = byte_str(s.recv(1024))
print(data)
timer = 0
@@ -335,7 +348,7 @@ if __name__ == '__main__':
array['time_10086'] = pingTime.get('10086')
array['tcp'], array['udp'], array['process'], array['thread'] = tupd()
s.send("update " + json.dumps(array) + "\n")
s.send(byte_str("update " + json.dumps(array) + "\n"))
except KeyboardInterrupt:
raise
except socket.error: