Macintosh Fan
Macintosh Fan

Reputation: 405

socket.close(fd) in Python. What does 'fd' mean?

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?

Upvotes: 1

Views: 2930

Answers (2)

slow-but-steady
slow-but-steady

Reputation: 1009

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!

Upvotes: 0

rahul kumar
rahul kumar

Reputation: 41

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.

Upvotes: 2

Related Questions