2

On Google IT automation with python specialization "Using python interact with Operating System"'s week 1 Qwiklabs Assessment: Working with Python 3rd module is not properly work

on ~/scripts directory : network.py code

    #!usr/bin/env python3
import requests
import socket

def check_localhost():
    localhost = socket.gethostbyname('localhost')
    print(localhost)
    if localhost == '127.0.0.1':
        return True

    return False
def check_connectivity():
    request = requests.get("http://www.google.com")
    responses = request.status_code
    print(responses)
    if responses == 200:
        return True

    return False

By using this code I make "Create a new Python module "

but Qwiklab show me that I cannot properly write code.!!!

now what is the problem?

8 Answers8

3

I am responding with this piece here because I noticed a lot of folks taking this course "Using Python to interact with the Operating System" on Coursera do have similar issues with writing the Python function check_localhost and check_connectivity. Please, copy these functions to your VM and try again. To ping the web and also check whether the local host is correctly configured, we will import requests module and socket module. Next, write a function check_localhost, which checks whether the local host is correctly configured. And we do this by calling the gethostbyname within the function. localhost = socket.gethostbyname('localhost') The above function translates a hostname to IPv4 address format. Pass the parameter localhost to the function gethostbyname. The result for this function should be 127.0.0.1. Edit the function check_localhost so that it returns true if the function returns 127.0.0.1.

import requests 
import socket 
#Function to check localhost
def check_localhost():
    localhost = socket.gethostbyname('localhost')
    if localhost == "127.0.0.1":
        return True
    else:
        return False

Now, we will write another function called check_connectivity. This checks whether the computer can make successful calls to the internet. A request is when you ping a website for information. The Requests library is designed for this task. You will use the request module for this, and call the GET method by passing a http://www.google.com as the parameter. request = requests.get("http://www.google.com") This returns the website's status code. This status code is an integer value. Now, assign the result to a response variable and check the status_code attribute of that variable. It should return 200. Edit the function check_connectivity so that it returns true if the function returns 200 status_code.

#Function to check connectivity
def check_connectivity():
    request = requests.get("http://www.google.com")
    if request.status_code == 200:
        return True
    else:
        return False

Once you have finished editing the file, press Ctrl-o, Enter, and Ctrl-x to exit. When you're done, Click Check my progress to verify the objective.

Ogah
  • 59
  • 2
3

I was also using a similar code, although It was getting executed fine in the lab terminal, however, it was not getting successfully verified. I contacted the support team using support chat and they provided a similar but relatively efficient code that worked ;

#!/usr/bin/env python3
import requests
import socket
localhost = socket.gethostbyname('localhost')
request = requests.get("http://www.google.com")
def check_localhost():
    if localhost == "127.0.0.1":
        return True
def check_connectivity():
    if request.status_code == 200:
        return True
1

I use the same code of yours to check what's the problem in the code, but your code successfully passed the qwiklabs check.

I think there is something wrong, Did you retry again by end this lab session and create another session just to check if this is something wrong with their end.

enter image description here

Ankit24007
  • 848
  • 9
  • 19
  • Thanks a lot, @Ankit24007. But this is a matter of sorrow that I try this qwiklab more than 7 times in 3 different device then I also see the youtube video. More interestingly this code is also successfully work for 4 th task of this assessment but unfortunately not work for this 3rd on. Every time I score 15/20 and end this lab :) – Md. Rakib Trofder Jun 27 '21 at 10:57
1

It's so easy, in this case, the shebang line would be /usr/bin/env python3.

you type a wrong shebang line:

#!usr/bin/env python3

But you should type:

#!/usr/bin/env python3

Add a shebang line to define where the interpreter is located.

0

It can be written like this

#!/usr/bin/env python3

import requests
import socket

def check_localhost():
    localhost = socket.gethostbyname('localhost')
    return localhost == "127.0.0.1"

def check_connectivity():
    request = requests.get("http://www.google.com")
    return request.status_code() == 200
lepsch
  • 8,927
  • 5
  • 24
  • 44
FengAn99G
  • 9
  • 1
-1

This script does work even if you are facing the issue in the post:

import requests
import socket
def check_localhost():
    localhost = socket.gethostbyname('localhost')
    return True # or return localhost == "127.0.0.1"
def check_connectivity():
    request = requests.get("http://www.google.com")
    return True #or return request==200

What could be wrong with your code? The verification system is faulty and doesn't accept:

-tab instead of 4 spaces as right identation

-spaces between lines

-1
#!usr/bin/env python3
import requests
import socket
def check_localhost():    
    localhost = socket.gethostbyname('localhost')
    print(localhost)
    if localhost == '127.0.0.1':
        return True
def check_connectivity():
    request = requests.get("http://www.google.com")
    responses = request.status_code()
    print(responses)
    if responses == '200':
        return True
  • The main Error is at responses=request.status_code() the brackets are must. – SUGANTH V Jan 29 '22 at 19:04
  • 1
    Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Jan 29 '22 at 20:28
-1
    #!/usr/bin/env python3

    import requests
    import socket

    def check_localhost():
        localhost = socket.gethostbyname('localhost')
        if localhost == "127.0.0.1":
            return True
    def check_connectivity():
        request = requests.get("http://www.google.com")
        if request.status_code() == 200:
            return True
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 22 '22 at 01:14