Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.freenet.ag!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'socket': 0.07; 'subject:missing': 0.07; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ternary': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'convenient': 0.24; 'exists': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'fri,': 0.33; 'proceed': 0.33; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'addresses,': 0.36; 'crazy': 0.36; 'connections': 0.38; 'easiest': 0.38; 'pm,': 0.38; 'sure': 0.39; 'subject:? ': 0.60; 'name': 0.63; '2015': 0.84; 'subject:here': 0.84; 'to:none': 0.92; 'connection,': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=SWTssKIlJgaN3fEI/GDEKJpJQR3u9wjfo4fSvISxqvw=; b=UHkIYlidi+yU2JNc6BP3CjHx930BwZJEV4rsewEFP9eOtT70lnqMtoAhgCXP1nO0Nh x8PjKRSa8G5Eu4DOoQfmfH9gb/TjZaiWHd+xQKznYA4BZxfWlFpfhUbduPb3aCVXRAG4 V03OLWr2AlLRDfzOkbC1yrzO+GzcG+O06DhThlebICEudgNVJ1hZsmcpU2SFHZ3DxI3c tpVDFRo4x35up6IxfbBcDeOFi/No8BbRNCWEX7NcOldeP/+VDr1cA+zyp7NBRYmVdh6J oJlfbWD8Zui+kaxUzSR2WwBcNiprhWNu267wdrjhsCr98xvWK8xHuJmJZrDV3lk3xvFQ EzjQ== MIME-Version: 1.0 X-Received: by 10.50.108.115 with SMTP id hj19mr9342210igb.34.1430475064738; Fri, 01 May 2015 03:11:04 -0700 (PDT) In-Reply-To: References: Date: Fri, 1 May 2015 20:11:04 +1000 Subject: Re: Am I missing something here? ipaddress vs socket From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430475067 news.xs4all.nl 2878 [2001:888:2000:d::a6]:50432 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89718 On Fri, May 1, 2015 at 7:42 PM, wrote: > Hi all, > > Given the following code: > > import ipaddress > import socket > > ip = ipaddress.ip_address(mystring) > sock_family = ip.???? > socket = socket.socket(sock_family, socket.SOCK_STREAM) > > Am I crazy or is this undoable? > > sock.AF_INET == 2 > sock.AF_INET6 == 10 > ip.version == 4 or 6 Are you trying to look up a name to get an address? Or just look up an address? The easiest way would be to use a ternary if: sock_family = sock.AF_INET if ip.version == 4 else sock.AF_INET6 But you may find it convenient to use a dedicated function for establishing a connection, which could look up an AAAA or A record for a name, then proceed through all addresses, attempting connections in turn. I'm fairly sure one exists in Python, but I can't right now remember the name. ChrisA