运行Ubuntu时Python代码中的回溯错误

时间:2014-12-07 13:38:53

标签: python sockets ubuntu

我编写的这段代码在Windows上运行正常但在我的Ubuntu 12.04上出现了一些错误。虽然代码表现良好,但它给出了一些我不想要的错误..请在这方面帮助我..

from socket import *
from threading import Thread
from Crypto.Cipher import AES
import os
import base64
import timeit
# Receiveing + Decoding the Information, symmetrical key isi

def clientHandler():
    conn, addr = s.accept()
    print addr, "is connected"
    while 1:
        data = conn.recv(1024)
        if not data:
            break
        print "Metering Data Received: Processing..."
        #creating decoding unpadding
        PADDING ="{"
        DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
        #creating a default key
        obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
        decrypted_data = DecodeAES(obj2,data)
        print decrypted_data


HOST = "" #localhost
PORT = 12000


s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)

print "Server is runnig"
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()

for i in range(4):
    Thread(target=clientHandler).start()

s.close() 

这是Ubuntu终端上出现的内容,但不是基于Windows的......

Server is runnig
Exception in thread Thread-4:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "chat_encrypt.py", line 10, in clientHandler
    conn, addr = s.accept()
  File "/usr/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
  File "/usr/lib/python2.7/socket.py", line 170, in _dummy
    raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor

1 个答案:

答案 0 :(得分:1)

for i in range(4):
    Thread(target=clientHandler).start()

s.close() 

最后一行关闭套接字...每个线程然后尝试接受'。