getSocket: Use returned family to create the socket

The existing code was parsing the passed in host to determine what type
of socket family to create.  getaddrinfo already provides this for us,
so there's no need to perform our own, potentially buggy, parsing.

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
James McCoy 2013-08-22 23:40:28 -04:00
parent caa36121a7
commit 88e4f73777

View File

@ -1,6 +1,6 @@
### ###
# Copyright (c) 2002-2005, Jeremiah Fincher # Copyright (c) 2002-2005, Jeremiah Fincher
# Copyright (c) 2011, James McCoy # Copyright (c) 2011, 2013, James McCoy
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -44,14 +44,10 @@ def getSocket(host):
"""Returns a socket of the correct AF_INET type (v4 or v6) in order to """Returns a socket of the correct AF_INET type (v4 or v6) in order to
communicate with host. communicate with host.
""" """
addrinfo = socket.getaddrinfo(host, None) addrinfo = socket.getaddrinfo(host, None,
host = addrinfo[0][4][0] socket.AF_UNSPEC, socket.SOCK_STREAM)
if isIPV4(host): family = addrinfo[0][0]
return socket.socket(socket.AF_INET, socket.SOCK_STREAM) return socket.socket(family, socket.SOCK_STREAM)
elif isIPV6(host):
return socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
raise socket.error, 'Something wonky happened.'
def isIP(s): def isIP(s):
"""Returns whether or not a given string is an IP address. """Returns whether or not a given string is an IP address.