'Why "Using python interact with Operating System"'s week 1 Qwiklabs Assessment: Working with Python code is not correctly working

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Solution 4:[4]

#!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

Solution 5:[5]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ogah
Solution 2 Ankit24007
Solution 3 Hubert Budny
Solution 4 SUGANTH V
Solution 5 Mohamed Yassin Jammeli