0

I have this piece of code here:

import socket
socket.close()

I get a warning that socket.close() is missing the 'fd' parameter. What integer should I fill in for general usage, and what do the integers mean?

Macintosh Fan
  • 355
  • 1
  • 4
  • 18
  • Yeah, but it doesn't explain in detail. The reason I am asking here on Stack Overflow is that I am not an advanced user. I just want a basic rundown on what it means and how I should use it. – Macintosh Fan Jul 07 '20 at 05:47
  • 3
    The package level `close(fd)` function requires a file descriptor of a open socket. So you need the `sock.file_no()` from a socket object to use in that function. The better option is to use the `sock.close()` function on the socket object. You wouldn't need to worry about the file descriptor, it's abstracted by the socket object. – rdas Jul 07 '20 at 05:50
  • _Which_ socket do you want to close? `fd` is the ID of _that_ socket. – DYZ Jul 07 '20 at 05:50
  • 1
    Read the docs: https://docs.python.org/3/howto/sockets.html – rdas Jul 07 '20 at 05:51
  • Edit: I tried sock.close() itself, and I got this: Traceback (most recent call last): TypeError: close() takes exactly one argument (0 given) – Macintosh Fan Jul 07 '20 at 05:52

2 Answers2

1

here fd is " File Descriptor" you must know how to handle file descriptor, the file descriptor fd (an integer as returned by a file object’s fileno() method) and build a socket object from the result.

0

Tried looking for a clear explanation but if you're in a bind, the answer mentioned by rdas is probably the best bet. To clarify, if you create a new socket called my_socket, then try doing my_socket.close(). Based on the official python documentation, try something like

# Echo client program
import socket
import sys

HOST = 'daring.cwi.nl'    # The remote host
# you need to indicate the host name here
# use
# HOST = socket.gethostbyname("")
# to use '0.0.0.0'
PORT = 50007              # The same port as used by the server
my_socket = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        my_socket = socket.socket(af, socktype, proto)
    except OSError as msg:
        my_socket = None
        continue
    try:
        my_socket.connect(sa)
    except OSError as msg:
        my_socket.close()
        my_socket = None
        continue
    break

While trying to figure this out, I also looked over this post, a lot of which echoes the information from the Disconnecting section from the Socket Programming HOWTO article.

Looking further into the sockets module documentation page, there seems to be one section that includes fd as an argument, but another section that doesn't... Not sure about this part, so can someone else clarify what;s going on here?

For some more information about sockets in general, check out this other SO answer and this quick slideshow on sockets and file descriptors.

I don't know the optimal solution here and don't understand everything either, so if I made a mistake or there are better resources comment below!

slow-but-steady
  • 961
  • 9
  • 15