Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'causing': 0.04; 'debugging': 0.07; 'socket': 0.07; '(its': 0.09; 'get.': 0.09; 'subject:()': 0.09; 'trailing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '"r")': 0.16; '"w")': 0.16; '(without': 0.16; 'edward': 0.16; 'guessing': 0.16; 'instead).': 0.16; 'main():': 0.16; 'manning': 0.16; 'presume': 0.16; 'received:74.125.82.178': 0.16; 'stdout': 0.16; 'prevent': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'code,': 0.22; 'input': 0.22; 'import': 0.22; '(in': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; "shouldn't": 0.24; 'why.': 0.24; 'fine': 0.24; 'helpful': 0.24; 'mon,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'generally': 0.29; 'reporting': 0.29; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'included': 0.31; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; 'occurs': 0.31; 'anyone': 0.31; 'file': 0.32; 'open': 0.33; 'url:python': 0.33; 'running': 0.33; 'guess': 0.33; 'received:74.125.82': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'doubt': 0.36; 'done': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'error.': 0.37; 'skip:& 10': 0.38; 'files': 0.38; 'issue': 0.38; 'pm,': 0.38; 'that,': 0.38; 'received:74.125': 0.39; 'does': 0.39; 'skip:p 20': 0.39; 'major': 0.40; 'url:mail': 0.40; 'ensure': 0.60; 'even': 0.60; 'above,': 0.60; 'is.': 0.60; 'skip:\xc2 10': 0.60; 'issues,': 0.61; 'our': 0.64; 'provide': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'results': 0.69; '8bit%:100': 0.72; 'jul': 0.74; '8bit%:46': 0.78; 'difference.': 0.84; 'subject:skip:g 10': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=CL5DhZyttey4TwyOF0Fm3c5SMKOdx+Q2Pv2twnByqAY=; b=IC8aYO7LUyna4YQLCJeCeNn5RZUt7murPDi7Jsg2z7cQ+S9fFXhJWuyztX7zDYy5Re hr5VNfMSJxLsilu3O4yizBkf72fC9AZh1JAx60bOL083FsBtIrBgbtNDZKDimYccL1Gj u9FKK+GRe5K+7THqElN+2NhBN89Ba1y5RakmVkIHFT+7YnZGJsjNusr8tz59Jh4/9Ymu 69SPrvvsS2tdK4i4mDR4kqyRbAbabnDNkA7WqkpcAQ98VUrclurgbkTJXHvW7leIhPcW qSg7pMfx7fK8GJK9rV/9t4VwY5LG5AC8rAqMQhjqrj0Exap3XHf6rxwgvhYCIsA+YS9I gMfw== X-Gm-Message-State: ALoCoQkVCu8RiI8dQuHDdL/Bna/StU3lzRJdfnQOs6TRFIh+5J5Un9CLTt0khLyr7N93yEU9S1Ku X-Received: by 10.180.105.68 with SMTP id gk4mr83123wib.24.1406583792491; Mon, 28 Jul 2014 14:43:12 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <52DDE47E-D858-4ACD-A5E8-04E77F5B2CA8@gmail.com> References: <52DDE47E-D858-4ACD-A5E8-04E77F5B2CA8@gmail.com> From: Chris Kaynor Date: Mon, 28 Jul 2014 14:42:52 -0700 Subject: Re: gethostbyaddr() To: Edward Manning Content-Type: multipart/alternative; boundary=f46d04426a604c7e0004ff47cff4 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 228 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406583798 news.xs4all.nl 2943 [2001:888:2000:d::a6]:39925 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75328 --f46d04426a604c7e0004ff47cff4 Content-Type: text/plain; charset=UTF-8 On Mon, Jul 28, 2014 at 2:33 PM, Edward Manning wrote: > I wrote this code, but it seem to work fine if I only have one ip in the > file. When I have more than one IP in the file > > I get a error. Does anyone have an idea why. > It would be helpful to know what the error you are getting is. It is also a good idea to generally provide the Python version and OS version with your messages, as they can often make a major difference. As it is, I've made my best guess below (in bold), I've also included some other notes that may cause issues, but I doubt are causing your error. > import socket > > > > > > def main(): > > > > # get file names > > infileName = input ("What file our the IP adderss in? ") > > outfileName = input("What file should the results go in?") > > > > # open files > > infile = open(infileName, "r") > > outfile = open(outfileName, "w") > While this shouldn't cause the issue your reporting (without other issues going on), Its generally a better idea to use the with statement to open file, like so: with open(infileName, "r") as infile: # code that needs infile goes here. This will ensure that the file is closed when you are done with it, even if an error occurs in the code that may prevent the code from running to completion. > > > > > #Proccess each line of the input file > > > > for line in infile: > > ipAddress = line.strip() > I'm guessing that, when you only have one IP, you do not have a trailing new-line in the file, but when you put more than one in the file, you have a trailing new-line. You can try adding: if not ipAddress: continue here to ignore any empty lines (due to the line.strip() above, it will also ignore all white-space lines) you might get. This may be the cause of your error... > resluts = socket.gethostbyaddr(ipAddress) > > print(resluts[0],resluts[2], end="") > Note that this does not seem to be printing into outfile (its printing to stdout instead). I presume this is for debugging purposes. > -- > https://mail.python.org/mailman/listinfo/python-list > > --f46d04426a604c7e0004ff47cff4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On M= on, Jul 28, 2014 at 2:33 PM, Edward Manning <ejmmanning@gmail.com= > wrote:

I wrote this code, but it seem to work fine if I only have one ip in the fi= le. When I have more than one IP in the file=C2=A0

I get a error. Does anyone have an idea why. =C2=A0 =C2=A0


It would be helpful to know what= the error you are getting is. It is also a good idea to generally provide = the Python version and OS version with your messages, as they can often mak= e a major difference. As it is, I've made my best guess below (in bold)= , I've also included some other notes that may cause issues, but I doub= t are causing your error.
=C2=A0

import socket

=C2=A0=

=C2=A0

def main():

=C2=A0

=C2=A0=C2=A0=C2=A0 # ge= t file names

=C2=A0=C2=A0=C2=A0 infileName =3D input ("What file our the IP adderss= in?=C2=A0 ")

=C2=A0=C2= =A0=C2=A0 outfileName =3D input("What file should the results go in?&q= uot;)

=C2=A0

=C2=A0=C2=A0=C2=A0 # open files

=C2=A0=C2=A0=C2=A0 infile =3D open(infileName, = "r")

=C2=A0=C2=A0=C2=A0 outfile =3D open(outfileName, "w")


While this shouldn't cause the issue you= r reporting (without other issues going on), Its generally a better idea to= use the with statement to open file, like so:
with open(infileName, "r") as infile:
=C2=A0 =C2= =A0 # code that needs infile goes here.

This will = ensure that the file is closed when you are done with it, even if an error = occurs in the code that may prevent the code from running to completion.
=C2=A0

=C2=A0

=C2=A0

=C2=A0=C2=A0=C2=A0 #Pro= ccess each line of the input file

=C2=A0

=C2=A0=C2=A0=C2=A0 for = line in infile:

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ipAddress =3D line.strip()


I'm guessing that, when you only h= ave one IP, you do not have a trailing new-line in the file, but when you p= ut more than one in the file, you have a trailing new-line. You can try add= ing:

if not ipAddress:
=C2=A0 =C2=A0 continue

here to ignore any empty lines (due to the line.strip(= ) above, it will also ignore all white-space lines) you might get. This may= be the cause of your error...
=C2=A0

=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 resluts =3D socket.gethostbyaddr(ipAddress)

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print(resluts[0],resluts[2], end= =3D"")


Note that this d= oes not seem to be printing into outfile (its printing to stdout instead). = I presume this is for debugging purposes.
=C2=A0
--
https://mail.python.org/mailman/listinfo/python-list


--f46d04426a604c7e0004ff47cff4--