Python Mutliprocessing/Sockets - 错误的文件描述符
Python Mutliprocessing/Sockets - Bad File Descriptor
我正在 Python 中编写服务器,客户端将向其发送命令(通过命令行 arg)"BYE"。 client/server 之间的连接此时应该终止了。 (我认为 socket.close() 会终止连接,但我不确定这是否正确)
我写了一个改编的回显服务器,其中 clientsocket.close() 在服务器迭代结束时。我希望只有在收到 BYE 时才会发生这种情况。
我不确定我的服务器程序的结构是否导致了这个问题(我还没有使用 getopt - 因为我仍然需要弄清楚)
服务器:
from multiprocessing import Process
import socket
import os
def handler(clientsocket, clientaddr):
print "Accepted connection from: ", clientaddr
while 1:
data = clientsocket.recv(1024)
if data:
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
# p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
msg = "You sent me: %s" % data
clientsocket.send(msg)
clientsocket.close()
return
if __name__ == "__main__":
TCP_IP = '127.0.0.1'
TCP_PORT = 5004
BUFFER_SIZE = 1024
print 'socket()'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'bind()'
s.bind((TCP_IP, TCP_PORT))
# f = open('recv.txt', 'wb')
print 'listen()'
s.listen(5)
workerProcesses =[]
while 1:
print "Server is listening for connections\n"
clientsocket, clientaddr = s.accept()
p = Process(target=handler, args = (clientsocket, clientaddr))
p.start()
# p.join()
workerProcesses.append(p)
serversocket.close()
当我将我的客户端socket.close() 放在if "BYE" 语句中时,我收到错误:套接字描述符错误。服务器的输出是:
data recieved:
BYE
Process Process-1:
Traceback (most recent call last):
File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap
self.run()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run
self._target(*self._args, **self._kwargs)
File "pyServ.py", line 26, in handler
clientsocket.send(msg)
File "/usr/lib64/python2.6/socket.py", line 167, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor
有没有关于为什么会这样的想法?
在你的 handler() 函数中你有这个:
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
#p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
msg = "You sent me: %s" % data
clientsocket.send(msg)
clientsocket.close()
return
if data == "BYE"
然后你发送 "BYE" 并关闭你的套接字......然后你打印 "data recieved" (打字错误):)
然后在 if
语句中调用 close()
。一旦在 if
语句之外,您尝试在刚刚关闭的那个套接字上再次 send()
数据。一旦关闭套接字,文件句柄就不再有效。你需要修改你的代码,这样你就不会在一个关闭的套接字上发送……我看到了两种明显的方法。
应该是
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
#p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
return
或
删除 if
语句中对 clientsocket.close() 的调用
我正在 Python 中编写服务器,客户端将向其发送命令(通过命令行 arg)"BYE"。 client/server 之间的连接此时应该终止了。 (我认为 socket.close() 会终止连接,但我不确定这是否正确)
我写了一个改编的回显服务器,其中 clientsocket.close() 在服务器迭代结束时。我希望只有在收到 BYE 时才会发生这种情况。
我不确定我的服务器程序的结构是否导致了这个问题(我还没有使用 getopt - 因为我仍然需要弄清楚)
服务器:
from multiprocessing import Process
import socket
import os
def handler(clientsocket, clientaddr):
print "Accepted connection from: ", clientaddr
while 1:
data = clientsocket.recv(1024)
if data:
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
# p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
msg = "You sent me: %s" % data
clientsocket.send(msg)
clientsocket.close()
return
if __name__ == "__main__":
TCP_IP = '127.0.0.1'
TCP_PORT = 5004
BUFFER_SIZE = 1024
print 'socket()'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'bind()'
s.bind((TCP_IP, TCP_PORT))
# f = open('recv.txt', 'wb')
print 'listen()'
s.listen(5)
workerProcesses =[]
while 1:
print "Server is listening for connections\n"
clientsocket, clientaddr = s.accept()
p = Process(target=handler, args = (clientsocket, clientaddr))
p.start()
# p.join()
workerProcesses.append(p)
serversocket.close()
当我将我的客户端socket.close() 放在if "BYE" 语句中时,我收到错误:套接字描述符错误。服务器的输出是:
data recieved:
BYE
Process Process-1:
Traceback (most recent call last):
File "/usr/lib64/python2.6/multiprocessing/process.py", line 232, in _bootstrap
self.run()
File "/usr/lib64/python2.6/multiprocessing/process.py", line 88, in run
self._target(*self._args, **self._kwargs)
File "pyServ.py", line 26, in handler
clientsocket.send(msg)
File "/usr/lib64/python2.6/socket.py", line 167, in _dummy
raise error(EBADF, 'Bad file descriptor')
error: [Errno 9] Bad file descriptor
有没有关于为什么会这样的想法?
在你的 handler() 函数中你有这个:
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
#p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
msg = "You sent me: %s" % data
clientsocket.send(msg)
clientsocket.close()
return
if data == "BYE"
然后你发送 "BYE" 并关闭你的套接字......然后你打印 "data recieved" (打字错误):)
然后在 if
语句中调用 close()
。一旦在 if
语句之外,您尝试在刚刚关闭的那个套接字上再次 send()
数据。一旦关闭套接字,文件句柄就不再有效。你需要修改你的代码,这样你就不会在一个关闭的套接字上发送……我看到了两种明显的方法。
应该是
if data == "BYE":
print "recvd BYE"
clientsocket.send("BYE")
clientsocket.close()
#p.join()
elif data == "DIR":
print "recvd DIR"
DIR = os.getcwd()
clientsocket.send(DIR)
print "data recieved:"
print data
return
或
删除 if
语句中对 clientsocket.close() 的调用