Error Errno 9 Bad File Descriptor
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of errno 9 bad file descriptor python this site About Us Learn more about Stack Overflow the company Business errno 9 socket Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask connect:errno=9 Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign raise error(ebadf, 'bad file descriptor') up What could be the reason for a socket error “[Errno 9] Bad file descriptor” up vote 5 down vote favorite I have a complex python (2.7.3) script which is trying to open a socket connection via self.socket.close() # doing something else self.socket.connect((host, port)) but all I get is the following socket error: error: [Errno 9] Bad file descriptor The host:port accepts connections
Python Subprocess Oserror Errno 9 Bad File Descriptor
as I have verified this with nc host port manually. So what could be the possible reasons I get this error for opening a connection to the given port, which actually works? I cannot and will not post the full script as it is too complex and irrelavent for this question. I just would like to know all possible reasons for this error, and how to check them and fix them. python sockets python-2.7 share|improve this question asked Jun 20 '13 at 6:53 Alex 7,6321667133 3 Read the documentation for socket.close(). You cannot reuse it in any operation (including connect) once you close it. It needs to be reinitialized with a call to socket.socket. –Thomas Jun 20 '13 at 6:58 add a comment| 1 Answer 1 active oldest votes up vote 18 down vote accepted You will need to create a new socket object. Maybe self.socket = socket.socket() after closing the previous socket and before connecting. share|improve this answer answered Jun 20 '13 at 6:58 Atmaram Shetye 661413 Bongo that's it! Thank you very much. –Alex Jun 20 '13 at 7:02 add a
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of
Errno Bad File Number
this site About Us Learn more about Stack Overflow the company Business Learn errno 9 bad file descriptor c more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question bad file descriptor python print x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up python http://stackoverflow.com/questions/17207051/what-could-be-the-reason-for-a-socket-error-errno-9-bad-file-descriptor socket.error: [Errno 9] Bad file descriptor up vote 2 down vote favorite I wanted to make a server and a client with Python. It was supposed to make multiple connections, one, where the server is sending something to the client, and one where the client is sending something to the server. The first connection worked fine, but the second one crashed with the http://stackoverflow.com/questions/38292142/python-socket-error-errno-9-bad-file-descriptor message: socket.error: [Errno 9] Bad file descriptor Here is the Server: import socket import errno import pickle def Main(): host = '188.174.233.99' port = 66666 all_text = ['text1', 'text2', 'text3'] all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n'] all_images = ['unlock.png', 'unlock.png', 'unlock.png'] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) s.bind((host, port)) s.listen(1) while True: c, addr = s.accept() c.setblocking(0) print "Connection from: " + str(addr) command = c.recv(1024) if command == 'GIVEALL': textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String c.send(textstring) else: try: new_event = pickle.loads(command) print new_event caption = new_event[0] image = new_event[1] describtion = new_event[2] city = new_event[3] except: pass try: c.close() s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) except socket.error as e: if e.errno != errno.ECONNRESET: raise pass if __name__ == '__main__': Main() And here is the Client: import socket import pickle from kivy.properties import StringProperty from kivy.properties import NumericProperty from kivy.properties import ListProperty class Netclient(object): def __init__(self): self.s = socket.socket() self.texte = [] self.current = 'All' self.ip = '188.174.233.99' self.port = 66666 def giveWid(self): print 'give Widgets executed' if self.current == 'All': self.texte, self.images, self.description = self.sentHOT(self.ip, self.port) return self.texte, self.images, self.description def sentHOT(self, host, port): self.s.connect((host, port)) self.s.send('GIVEALL')#send
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://stackoverflow.com/questions/28495956/python-socket-programming-bad-file-descriptor-error workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. bad file Join them; it only takes a minute: Sign up Python Socket Programming - Bad file descriptor error up vote 1 down vote favorite I'm trying to create a simple server, and keep getting the following error in IDLE: File "C:\Python27\lib\socket.py", line 202, in accept sock, addr = self._sock.accept() File "C:\Python27\lib\socket.py", line 170, in _dummy raise error(EBADF, 'Bad file descriptor') bad file descriptor error: [Errno 9] Bad file descriptor This is my code. I've tried understanding why, and it has something to do with closing one of the sockets and then trying to use it again but I don't really get how I'm supposed to fix it. Any and all help is appreciated. :) from socket import * serverSocket = socket(AF_INET, SOCK_STREAM) port=10101 serverSocket.bind(('',port)) serverSocket.listen(5) while True: print 'Ready to serve...' connectionSocket, addr = serverSocket.accept() try: message = serverSocket.recv(1024) filename = message.split()[1] f = open(filename[1:]) outputdata = f.read() connectionSocket.send("HTTP/1.1 200 OK\r\n\n") for i in range(0, len(outputdata)): connectionSocket.send(outputdata[i]) connectionSocket.close() except IOError: connectionSocket.send('HTTP/1.1 404 File not found\r\n\n') connectionSocket.close() serverSocket.close() serverSocket.close() python sockets tcp share|improve this question asked Feb 13 '15 at 9:08 Gustaf Svensson 14819 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote A couple of things: As user27994550 put in his code, you want to use message = connectionSocket.recv(1024) to receive messages, not the server socket. The other thing that both of your codes missed is you don't want to call serverSocket.