Lars
Lars

Reputation: 1270

How to see if user is connected to internet or NOT?

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

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

Upvotes: 0

Views: 737

Answers (1)

KlausF
KlausF

Reputation: 172

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'

Upvotes: 1

Related Questions