我正在测试一些脚本,并且在客户端脚本上发现了空错误回溯:
=========== RESTART: /home/pi/Desktop/Pythonic/Chat/ReadClient.py ===========
Enter name: SuperUser
Enter server port: 5162
> Traceback (most recent call last):
我不知道这是什么意思,下面是两个脚本: 我在想的是,Python默认IDE认为它发现了一个错误,但无法识别此错误类型。因此它返回一个空的追溯。
客户端脚本
import os, threading
from socket import *
name = input('Enter name: ')
bufsiz = 1024
host = 'localhost'
port = int(input('Enter server port: '))
addr = (host, port)
client = socket(AF_INET , SOCK_STREAM)
##try:
client.connect(addr)
##except socket_error as serr:
## if serr.errno != errno.ECONNREFUSED:
## raise serr
## else:
## print('Invalid Port number:[111]')
## port = int(input('Enter server port: '))
## addr = (host, port)
## client.connect(addr)
# sending name
client.sendto(name.encode('utf-8'),addr)
os.system('cls' if os.name == 'nt' else 'clear')
def input_loop():
global data, client
while True:
data = input('> ')
if not data:
client.sendto('/disconnect'.encode('utf-8'),addr)
break
else:
client.sendto(data.encode('utf-8'),addr)
def recv_loop(client,caddr):
while True:
data = client.recv(1024).decode('utf-8')
if data != 'n_dat':
if data != '/ping':
print(data)
else:
client.sendto('/ping'.encode('utf-8'),caddr)
print('pinged by server')
threads = []
dat_thread = threading.Thread(target=input_loop, args=())
dat_thread.start()
thread = threading.Thread(target=recv_loop, args=(client,caddr))
thread.start()
服务器脚本:
import os, socket, time, threading, random
class Server:
def __init__(self,host,port,user):
self.port = port
self.host = host
self.user = user
self.bufsize = 1024
self.addr = (host,port)
self.socket = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
self.socket.bind(self.addr)
print("Server running on",host,"at port",port)
self.socket.listen(5)
class rank:
def __init__(self, level):
self.level = level
def getPerms(l):
return (l==6,l>=6,True,l>=3,l>=2,l>=5,l>=4,l>=3,l>=2,l>=5>l>=6)
def getPerms(level,action):
return getperms(level)[action]
def getLevel(level):
if(level == 1):
return 'Guest'
if(level == 2):
return 'Experienced'
if(level == 3):
return 'Veteran'
if(level == 4):
return 'Donor'
if(level == 5):
return 'Mod'
if(level == 6):
return 'Admin'
if(level == 7):
return 'Owner'
else:
return False
# level scale
# A = Server control
# B = Ban
# C = Chat
# D = Direct Message
# H = Vanish
# I = ignore
# K = Kick
# M = Server mute
# T = Title
# V = View members
# U = Unban
## A B C D I K M T V H U
## _____________________________________
## 1 | Guest | | |X| | | | | | | | |
## 2 | Experienced | | |X| |X| | | |X| | |
## 3 | Veteran | | |X|X|X| | |X|X| | |
## 4 | Donor | | |X|X|X| |X|X|X|X| |
## 5 | Mod | | |X|X|X|X|X|X|X|X| |
## 6 | Admin | |X|X|X|X|X|X|X|X|X|X|
## 7 | Owner |X|X|X|X|X|X|X|X|X|X|X|
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def recv_loop(server,client,caddr):
print ('Connected To',caddr)
while True:
global clients
global ping_count
name = clients[client]
data = client.recv(1024)
namecheck = getrank(caddr[0],name,)
formattime = time.strftime(time.strftime("%H:%M:%S",time.gmtime()))
if not data:
break
if data.decode('utf-8') == '/disconnect':
print(formattime + ' : ' +'[' + namecheck + ']'+ name +' has disconnected ')
break
if data.decode('utf-8') == '/ping':
pings = 0
mssg = str(formattime + ' : ' +'[' + namecheck + ']'+ name +': '+ data.decode('utf-8'))
print(formattime + ' : ' +'[' + namecheck + ']'+ name +': '+ data.decode('utf-8'))
for c in clients:
if c != client:
c.send(mssg.encode('utf-8'))
else:
c.send(bytes('n_dat','utf-8'))
log(name, data.decode('utf-8'),getrank(caddr[0],name))
client.close()
def log(name, msg, rank):
with open('log.txt', 'a') as log:
log.write(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())+ ' ' + name + ': ' + msg + '\n')
def getrank(ip, name):
with open('ranks.txt', 'r') as ranks:
for line in ranks:
addr = line.split('$',1)[0]
if addr == ip:
rank = (line.split('$$',1)[0]).split('$')[1]
return rank
print('new user')
writerank(ip,name)
return ' Guest '
def writerank(ip,name):
with open('ranks.txt','a') as file:
file.write(ip + '$' + ' Guest ' + '$$' + name + '$$$' + '0/n')
def ping(client, ping_rate):
global ping_count
while True:
time.sleep(60/ping_rate)
client.send(bytes('/ping','utf-8'))
print('pinging client ' + str(client))
ping_count = ping_count + 1
##def getname(caddr):
## with open('ranks.txt', 'r+') as ranks:
## for line in ranks:
## addr = line.split(' .R. ')[0]
## if addr == caddr:
## name = line.split(' ... ')[1]
## return bytes(name,'utf-8')
##
host = 'localhost'
port = random.randint(5000,6000)
user = 'No one'
name = input('Server name : ')
ping_rate = 6
server = Server(host, port, user)
clients = {}
threads = []
ping_count = 0
while True:
client, caddr = server.socket.accept()
# name extraction
name = client.recv(1024)
#name = getname(caddr)
clients[client] = name.decode('utf-8')
pingthread = threading.Thread(target=ping, args=(client,ping_rate))
thread = threading.Thread(target=recv_loop, args=(server,client, caddr))
pingthread.start()
thread.start()
有人知道为什么会发生这种空引用吗? 如果是这样,如何避免?
答案 0 :(得分:0)
我似乎找到了答案:
> Traceback (most recent call last):Hello\
> File "/home/pi/Desktop/Pythonic/Chat/ReadClient.py", line 58, in <module>
thread = threading.Thread(target=recv_loop, args=(client,caddr))
Uhh
> NameError: name 'caddr' is not defined
因此,事实证明,回溯是三个独立的打印功能,但是由于我要进行输入循环,所以它将打印延迟到输入得到满足为止
如何复制:
import threading
def InputThread():
global data
while True:
data = input('> ')
inputThread = thread.Thread(target=InputThread,args=())
inputThread.start()
raise Exception