How to check internet connection with a guarantee that the checking will not fail in the event that the Internet is connected but slow Mobile data - python-requests

Welcome everyone I use this code to check that the internet is connected
import requests
def check():
try:
request = requests.get("http://www.example.com/", timeout=15)
return True
except (requests.ConnectionError, requests.Timeout) as exception:
return False
But sometimes the connection time ends with the presence of the Internet Because I download some movies or games and so it gives me a false report that the internet is not connected because of the slow internet
Is there a server or something that can check the internet if it is online or not Even if the internet is very slow?
Note that I use mobile data, not WiFi
have tried to change the operation failed
Where I programmed a code when it finds that there is no internet if there are no directions to the server
gives a value of false
The operation also failed

Related

Why is my internet connection not stable?

After connecting, I have no internet connection in several cases. The status indicator does not write an error, it shows that the Internet is available, but the websites do not load. I took one pictures with Wireshark when the error exists.
I try Driver reinstallation, connection reset, but the problem is same.

Check whether there is an Network connection available on pure dart

Is there a way in dart to get notified (listen to) when the network disconnecting and do action only if it off?.
I do not want to check internet connection so no need for pinging google.com (connection to router without internet is still good).
Searching for simple way that interact with the network interfaces of the device and listen to change or return True/False about the network state.
In worse case I can use Process.run(...) with bash commend but this ugly fix.
No flutter here only pure dart (dart Console Application).
If you are running the program on linux you can use bash commend like this
// Check if connected to network, if there is a connection than return network name
Future<String> getConnectedNetworkName() async {
return await Process.run('iwgetid',
['-r']).then((ProcessResult results) {
print(results.stdout.toString());
return results.stdout.toString().replaceAll('\n', '');
});
}
This will return network name even if the network does not connected to the internet

TcpClient/NetworkStream not detecting disconnection

I created a simple persistent socket connection for our game using TcpClient and NetworkStream. There's no problem connecting, sending messages, and disconnecting normally (quitting app/server shuts down/etc).
However, I'm having some problems where, in certain cases, the client isn't detecting a disconnection from the server. The easiest way I have of testing this is to pull out the network cable on the wifi box, or set the phone to airplane mode, but it's happened in the middle of a game on what should otherwise be a stable wifi.
Going through the docs for NetworkStream etc, it says that the only way to detect a disconnection is to try to write to the socket. Fair enough, except, when I try, the write passes as if nothing is wrong. I can write multiple messages like this, and everything seems fine. It's only when I plug the cable back in that it sees that it's disconnected (all messages are buffered?).
The TcpClient is set to NoDelay, and there's a Flush() called after every write anyway.
What I've tried:
Writing a message to the NetworkStream - no joy
CanWrite, Connected, etc all return true
TcpClient.Client.Poll( 1000, SelectMode.SelectWrite ); - returns true
TcpClient.Client.Poll( 1000, SelectMode.SelectRead ) && TcpClient.Client.Available == 0 - returns true
TcpClient.Client.Receive(buffer, SocketFlags.Peek) == 0 - when connected, blocks for about 10-20s, then returns true. When no server, blocks forever(?)
NetworkStream.Write() - doesn't throw an error
NetworkStream.BeginWrite() - doesn't throw an error (not even when calling EndWrite())
Setting a WriteTimeout - had no effect
Having a specific time where we haven't received a message from the server (normally there's a keep-alive) - I had this, but removed it, as we were getting a lot of false-positives due to lag etc (some clients would see between 10-20s of lag)
So am I doing something wrong here? Is there any way to get the NetworkStream to throw an error (like it should) when writing to a socket that should be disconnected?
I've no problem with a keep-alive (the default case is the server will notify the client that it hasn't received anything in a while, and the client will send a heartbeat), but at the minute, according to the NetworkStream everything's hunky-dory.
It's for a game, so ideally the detection should be quick enough (as the user can still move through the game until they need to make a server call, some of which will block the UI, so the game seems broken).
I'm using Unity, so it's .Net 2.0
is to pull out the network cable on the wifi box
That's a good test. If you do that the remote party is not notified. How could it possibly find out? It can't.
when I try, the write passes as if nothing is wrong
Writes can (and are) buffered. They eventually enter a block hole... No reply comes back. The only way to detect this is a timeout.
So am I doing something wrong here?
You have tried a lot of things but fundamentally you cannot find out about disconnects if no reply comes back telling you that. Use a timeout.

Why does youtube googleapis redirect to a network attack message?

For the past few days, YouTube links that worked before such as this have been giving me this error message:
Your connection is not private
Attackers might be trying to steal your information from
www.youtube.googleapis.com (for example, passwords, messages, or
credit cards).
www.youtube.googleapis.com normally uses
encryption to protect your information. When Chrome tried to connect
to www.youtube.googleapis.com this time, the website sent back unusual
and incorrect credentials. Either an attacker is trying to pretend to
be www.youtube.googleapis.com, or a Wi-Fi sign-in screen has
interrupted the connection. Your information is still secure because
Chrome stopped the connection before any data was exchanged.
You cannot visit www.youtube.googleapis.com right now because the
website uses HSTS. Network errors and attacks are usually temporary,
so this page will probably work later.
NET::ERR_CERT_COMMON_NAME_INVALID
Are other people seeing a network attack message with the link above, or is it just an issue with my local router? It occurs not only in Chrome but Firefox and Explorer (on two laptops that I tested) What should I do?

DDP between two servers doesn't reconnect

I have two meteor applications connected via DDP on different servers and server A send data to server B. This is the way they work.
Server A
Items = new Meteor.Collection('items');
Items.insert({name: 'item 1'});
if (Meteor.isServer) {
Meteor.publish('items', function() {
return Items.find();
});
}
Server B
var remote = DDP.connect('http://server-a/');
Items = new Meteor.Collection('items', remote);
remote.subscribe('items');
Items.find().observe({
added: function(item) {
console.log(item);
}
});
Every time I call Items.insert(something) on server A, on Server B I got a log on the console with the object I saved on Server A. But if Server B lost Internet connection, the data inserted on Server A doesn't appear anymore on Server B when it reconnect to Internet.
Server B is connected to Internet through a router. This problem only happen when I disconnect and reconnect the router, not when I disconnect and reconnect the server from the router. Both servers are on different networks and connect via Internet.
I created a timer on Server B that call remote.status() but always get { status: 'connected', connected: true, retryCount: 0 } when connected or disconnected from Internet.
Update: steps to reproduce
I created a project on github with the testing code https://github.com/camilosw/ddp-servers-test. Server A is installed on http://ddpserverstest-9592.onmodulus.net/
My computer is connected to Internet through a wireless cable modem.
Run mrt on server-b folder
Go to http://ddpserverstest-9592.onmodulus.net/ and click the link Insert (you can click delete to remove all previous inserts). You must see a message on your local console with the added item.
Turn off the wireless on the computer and click the insert link again. (You will need to click on another computer with Internet access, I used an smartphone to click the link)
Turn on the wireless on the computer. You must see a message on your local console with the second item.
Now, turn off the cable modem and click the insert link again.
Turn on the cable modem. This time, the new item doesn't appear on the console.
I also did it with an android smartphone using the option to share Internet to my computer via wireless. First I turned off and on the wireless on my computer and worked right. Then I turned off and on the Internet connection on the smartphone and I got the same problem.
Update 2
I have two wireless router on my office. I found that the same problem happen if I move between routers.
Emily Stark, from the Meteor Team, confirmed that this is due to a missing feature on the current implementation (version 0.7.0.1 at the moment I write this answer). Their answer is here https://github.com/meteor/meteor/issues/1543. Below is their answer and a workaround she suggest:
The server-to-server connection is not reconnecting because Meteor currently doesn't do any heartbeating on server-to-server DDP connections. Just as in any other TCP connection, once you switch to a different router, no data can be sent or received on the connection, but the client will not notice unless it attempts to send some data and times out. This differs from browser-to-server DDP connections, which run over SockJS. SockJS does its own heartbeating that we can use to detect dead connections.
To see this in action, here is some code that I added to server-b in your example:
var heartbeatOutstanding = false;
Meteor.setInterval(function () {
if (! heartbeatOutstanding) {
console.log("Sending heartbeat");
remote.call("heartbeat", function () {
console.log("Heartbeat returned");
heartbeatOutstanding = false;
});
heartbeatOutstanding = true;
}
}, 3000);
remote.onReconnect = function () {
console.log("RECONNECTING REMOTE");
};
With this code added in there, server-b will reconnect after a long enough time goes by without an ACK from server-a for the TCP segments that are delivering the heartbeat method call. On my machine, this is just a couple minutes, and I get an ETIMEDOUT followed by a reconnect.
I've opened a separate task for us to think about implementing heartbeating on server-to-server DDP connections during our next bug week. In the meantime, you can always implement heartbeating in your application to ensure that a DDP reconnection happens if the client can no longer talk to the server.
I think you are not passing DDP connection object to the Collection correctly, try:
var remote = DDP.connect('http://server-a/');
Items = new Meteor.Collection('items', { connection: remote });
It might be useful for debugging to try all these connection games from the browser console first, since Meteor provides the same API of connection/collections on the client (except for the control flow). Just open any Meteor application and try this lines from the console.
I revised a sample of communication between two ddp server, based on camilosw's code.
Server A as Cloud Data Center. Server B as Data Source, if some data changed, should be send to Server A.
You can find the code from https://github.com/iascchen/ddp-servers-test

Resources