简体   繁体   中英

AttributeError: '_socketobject' object has no attribute 'error'

I'm using python 2.7. In that code, listen_list is a list with UDP and TCP sockets, and the error must be happen when the TCP socket is closed because client disconnects from server. I don't know because in the other parts of the program the errors is OK but in this line I get that error.

import sys #for exit
import socket #for sockets
import select

listen_list = copy.copy(UDPlist) #list with listening udp sockets
listen_list.append(mySocket) #mySocket is a TCP socket for connect with client
try:
    rlist, wlist, elist = select.select(listen_list, [], [], 5)
except socket.error:
   print 'Failed. There is some socket that is invalid'
   listen_list = delete()
   continue

When I run the server, it works until it hits this error

File "server.py", line 67, in <module>
except socket.error:
AttributeError: '_socketobject' object has no attribute 'error'

The _socketobject is the type referred to by socket.socket . The error message shows that somewhere in the code, socket was bound to an instance of socket.socket , shadowing the module.

>>> import socket
>>> socket = socket.socket()   # this happens somewhere in your code
>>> socket.error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '_socketobject' object has no attribute 'error'

Remove the assignment to socket , eg chose a different name, to make the module and its content accessible.

>>> import socket
>>> some_socket = socket.socket()   # this should be done instead
>>> socket.error
<class 'socket.error'>

As stated in the docs, socket.error was deprecated since Python 3.3 and OSError is raised instead.

starting from Python 3.3, errors related to socket or address semantics raise OSError or one of its subclasses (they used to raise socket.error).

Check https://docs.python.org/3/library/socket.html for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM