0

I am building a BlogApp and I am stuck on a Problem.

What i am trying to do

I am trying to implement a feature that ,`If user is connected to internet then everything works fine BUT if user is not connected to internet then show a message like "You're not Connected to Internet".

What have i tried

  • I also tried channels but then i think internet connection are far away from Django-channels.

  • I also tried this :

    url = "http://127.0.0.1:8000/"
    timeout = 5
    try:
        request = requests.get(url, timeout=timeout)
        print("Connected to the Internet")
    except (requests.ConnectionError, requests.Timeout) as exception:
        print("No INTERNET")
    

But it is keep showing me :

'Response' object has no attribute 'META'

I don't know what to do.

Any help would be Appreciated.

Thank You in Advance

Lars
  • 1,234
  • 1
  • 8
  • 31
  • [Here](https://github.com/statsmodels/statsmodels/blob/d42dc3f3c63edf0e2eb08f9297705ed9333c4357/statsmodels/datasets/utils.py#L274-L281) is an implementation that uses `urllib.request.urlopen`. – Mustafa Aydın Apr 07 '21 at 05:57
  • 1
    a simple workaround will be to use js... check ---- " https://stackoverflow.com/questions/189430/detect-the-internet-connection-is-offline " – SANGEETH SUBRAMONIAM Apr 07 '21 at 06:00

1 Answers1

1

It is not easy to know whether you're connected to the internet. In fact it is not even clear what this means. It depends a lot on the context.

In many practical cases it means, that your network setup is setup such, that you can access a DNS server and that you can access at least one machine on the internet.

You could just use one known url like for example "https://google.com" or "https://stackoverflow.com".

However this means that:

  • your test will fail if given service is for any reason down
  • you create requests to a server that isn't yours.

If you know, that the application should access your special web service, then you could use the url of your special web service:

url = "https://your_special_webservice.yourdomain"

Side information:

If you put the code in your question into a django view, that handles http requests, then you should probably write something like:

request = requests.get(url, timeout=timeout)

instead of

response = requests.get(url, timeout=timeout)

Otherwise you will overwrite the request object, of your django view and this is probably what provoked your error message: 'Response' object has no attribute 'META'

KlausF
  • 152
  • 8
  • You're right, BUT now i think i should use `JavaScript` for check the weather of Internet Connection . – Lars Apr 07 '21 at 06:08
  • using javascript will move the test for internet conection from the web server into your web browser. depending on what you want to achieve exactly, this might be a good idea. (You might want to adapt the question to reflect this) – KlausF Apr 07 '21 at 06:10
  • Yes, Thank you Very Much for you Effort – Lars Apr 07 '21 at 06:11