VPfB
VPfB

Reputation: 17237

Can socket.getaddrinfo port number be given as a string?

These are normal getaddrinfo uses:

socket.getaddrinfo('localhost',25)
socket.getaddrinfo('localhost','smtp')

but this works as well (tried in Python 3.4):

socket.getaddrinfo('localhost','25')

Seems logical to me, but the documentation says:

port is a string service name such as 'http', a numeric port number or None

Can a string like '25' be considered numeric? Is the last getaddrinfo example OK?

Upvotes: 2

Views: 983

Answers (1)

interjay
interjay

Reputation: 110069

The Python socket module is a wrapper around the C API for the BSD socket interface. In this interface, the getaddrinfo function takes a service parameter which is a string that can be either a service name, a string representation of a port number, or null.

As seen here, the Python function converts the corresponding argument to a string (if needed) and calls the C getaddrinfo function. So it will work equivalently with both 25 and '25'.

Upvotes: 3

Related Questions