LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem inserting code into python function, works in console when typed out (https://www.linuxquestions.org/questions/programming-9/problem-inserting-code-into-python-function-works-in-console-when-typed-out-4175625173/)

emetib 03-07-2018 08:39 PM

problem inserting code into python function, works in console when typed out
 
so i'm trying to get something to work in a function and i don't know if it's a timing issue or what it might be.

in the console (python 3.6.4+, debian) ->

Code:

>>> import socket
>>> ip_addr = socket.gethostbyname('debian.org')
>>> print(ip_addr)
130.89.148.14

yet when i'm putting it into a function ->
(i've put the print() statements in to try and debug what is going on.)

Code:

import socket
def socket_return():
    with open('One_ip') as One_ip:
        for line in One_ip:
            print(line)
            ip_addr = socket.gethostbyname(line)
            print(ip_addr)
print(socket_return())

so when i run this i keep getting ->

Code:

'google.com'

    domain = socket.gethostbyname(ip_addr)
socket.gaierror: [Errno -2] Name or service not known

i've read the doc on the socket.gaierror and half understand it. i've also tried to put time.sleep(couple of seconds) after the ip_addr = ... line to try and wait for a response, yet don't know why this is not working.

hopefully someone can help with this.

contents of 'One_ip'(just a basic text file) ->

'google.com'
'debian.org'
'redhat.com'

Sefyir 03-07-2018 09:04 PM

You don't need the quotes in the file ('debian.org')
'' just represents a string in python. Remove them from the One_ip file.
To compare, when you typed it into the console, it got 'debian.org', however from the file it read it as ''debian.org'', a string that that quotes around it.
It's also probably getting confused since there might be some newline there
This fixes it -> .strip()
Code:

import socket
def socket_return(input_file):
    with open(input_file) as One_ip:
        for line in One_ip:
            print(line)
            ip_addr = socket.gethostbyname(line.strip())
            print(ip_addr)
print(socket_return(my_file))

More functional way of doing it:
Code:

#!/usr/bin/env python3                                                         
                                                                               
import socket                                                                 
my_file = 'One_ip'                                                           
def socket_return(input_file): 
    url_ip = list()                                                         
    with open(input_file) as One_ip:                                             
        for line in One_ip:                                                                                                     
            ip_addr = socket.gethostbyname(line.strip())                       
            url_ip.append((line.strip(), ip_addr))
            return url_ip                            
for url, ip in socket_return(my_file):                                                 
    print(url, ip)

Or to show results as soon as they show up
Code:

#!/usr/bin/env python3                                                         
                                                                               
import socket                                                                 
my_file = 'One_ip'                                                             
def socket_return(input_file):                                                 
    with open(input_file) as One_ip:                                           
        for line in One_ip:                                                   
            print(line)                                                       
            ip_addr = socket.gethostbyname(line.strip())                       
            yield ip_addr                                                     
for value in socket_return(my_file):                                           
    print(value)


emetib 03-07-2018 10:29 PM

thanks Sefyir for your response.

so i tried the last one first, and it was only returning a 'generator object' which didn't work with the continuation of the script (not described).

the first one works, didn't use a list, just the ip address is needed for what i want to do.

thanks for pointing out the .strip() and the need to not need the quotes, when reading my 'python pocket reference' i didn't see that it would return the \n at the end.

take care.
em


All times are GMT -5. The time now is 06:22 AM.