LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2018, 08:39 PM   #1
emetib
Member
 
Registered: Feb 2003
Posts: 484

Rep: Reputation: 33
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'

Last edited by emetib; 03-07-2018 at 08:40 PM. Reason: spelling
 
Old 03-07-2018, 09:04 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 579

Rep: Reputation: 267Reputation: 267Reputation: 267
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)

Last edited by Sefyir; 03-07-2018 at 09:11 PM.
 
2 members found this post helpful.
Old 03-07-2018, 10:29 PM   #3
emetib
Member
 
Registered: Feb 2003
Posts: 484

Original Poster
Rep: Reputation: 33
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
 
  


Reply

Tags
python3, socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Learning Python, how should I implement the following problem in Python 3 code? rblampain Programming 8 05-29-2016 10:53 AM
[SOLVED] Python 2.6 code behaves differently inside function than outside it dracuss Programming 2 03-21-2011 12:51 PM
Problem with Python code under Linux dmsynck Programming 1 10-25-2007 10:18 PM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
Inserting void function into Linux Kernel 2.6.17.14 (Where to insert?) vmt1 Programming 2 04-29-2007 08:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration