Home > other >  Raspberry pie, python socket bolt reconnection
Raspberry pie, python socket bolt reconnection

Time:09-19

 # - * - coding: utf-8 - * - 
The import socket
The import time

Try:
SERVER_IP="123.212.125.132"
SERVER_PORT=6811
Server_addr=(SERVER_IP SERVER_PORT)
Socket_tcp=socket. The socket (socket. AF_INET, socket. SOCK_STREAM)
Socket_tcp. Setsockopt (socket. SOL_SOCKET, socket SO_KEEPALIVE, 1)

While True:
Try:
Print (" Connecting to the server @ % s: % d... "% (SERVER_IP SERVER_PORT))
Socket_tcp. Connect (server_addr)
Break
Except the socket. Error:
Print (" Can 't connect to the server, try it latter!" )
Time. Sleep (1)
The continue
Print (" identifiers package... ") # socket connection is successful, waiting for receiving information

While True:
Try:
Number=socket_tcp. Send (" hello world ")
Print the number
Time. Sleep (5)
Except the socket. Error:
Print (" socket error ")
Except:
Other print (" error ")
Except:
Pass



You take a look at the code, the code I mainly as a client socket, link to a remote server, every 5 seconds to the remote server sends a "hello world", in the process of sending, if there is a socket link error, is output on the screen "socket error",
Now, so I use python debugger PyCharm on desktop computers to run the program, after normal link socket, assuming that the network error first, such as disconnecting or have no network, then by definition the socket should be disconnected, if I pull the cable after about 30 seconds, PyCharm only output "socket error",
Based on the computer side, I copy the same program to "" raspberry pie" "(raspberries pie is own raspbian) system to run on the same case, such as disconnecting, passes through more than 20 minutes to output" socket error ",
I want now is to at the time of the socket connection errors, such as pull the cable, network error caused by such as socket connection error, able to quickly detect the socket connection error, then the socket connection, the big question now is do not know how to monitor the socket state of link,, every brother please give advice or comments! ~

CodePudding user response:

I also come across this problem, did not delve into, directly into a short connection

CodePudding user response:

Use settimeout (5)
If 5 seconds no reaction, obtaining an exception is thrown, prompt,

CodePudding user response:

refer to the second floor seakingx response:
use settimeout (5)
If 5 seconds no reaction, obtaining an exception is thrown, prompt,


. I just added a line of code socket_tcp settimeout (5), I've just tried once, also is not, is a normal link, pull the cable or the same situation,


CodePudding user response:

reference 1st floor ipqtjmqj response:
I also come across this problem, did not delve into, directly into a short connection


I think can further study the appropriately, because the socket is quite extensive

CodePudding user response:

reference qq_20656883 reply: 3/f
Quote: refer to the second floor seakingx response:

Use settimeout (5)
If 5 seconds no reaction, obtaining an exception is thrown, prompt,


. I just added a line of code socket_tcp settimeout (5), I've just tried once, also is not, is a normal link, pull the cable or the same situation,




After 5 seconds program not throw an exception?

CodePudding user response:

reference 5 floor seakingx reply:
Quote: refer to the third floor qq_20656883 response:

Quote: refer to the second floor seakingx response:

Use settimeout (5)
If 5 seconds no reaction, obtaining an exception is thrown, prompt,


. I just added a line of code socket_tcp settimeout (5), I've just tried once, also is not, is a normal link, pull the cable or the same situation,




After 5 seconds program not throw an exception?


No exception is thrown, it took about 30 seconds to throw an exception

CodePudding user response:

 sk=socket. The socket (socket. AF_INET, socket. SOCK_STREAM) 
Sk. Settimeout (3)
Try:
Sk. Connect ((1521) '10.202.1.1')
Print 'Server port 1521 OK! '
Except the Exception:
Print 'Server port 1521 not connect! '
Return ""
Sk. The close ()


I in the connection prior to the ORACLE server is open, basic it is 3 seconds to return to the state of the server,

CodePudding user response:

refer to 7th floor seakingx response:
 sk=socket. The socket (socket. AF_INET, socket. SOCK_STREAM) 
Sk. Settimeout (3)
Try:
Sk. Connect ((1521) '10.202.1.1')
Print 'Server port 1521 OK! '
Except the Exception:
Print 'Server port 1521 not connect! '
Return ""
Sk. The close ()


I in the connection prior to the ORACLE server is open, basic it is 3 seconds to return to the state of the server,


Your mind should be like this: the link state of judging the socket is using sk. Connent () to determine, in this case, suppose I want to check the state of the socket every 1 minute to sk. Connet, such an approach is not good?

CodePudding user response:

The requirement is on my side sometimes because of the network or database server, ORACLE connection, will spend nearly 1 minute to have a result, so use this socket to determine first server status,
Only problem solving method, ok I can tell,

CodePudding user response:

In addition, I see see documents mentioned KEEPALIVE way, timeout will be very long, may be the impact for abnormal return time, suggest you try the 1st floor said short connection,

CodePudding user response:

I also encountered this problem, under the Windows 7 and Win10 are the same, if it is directly inserted in the phone cable, a timeout exception is thrown, if through the router, wait for about 30 seconds to respond.
  • Related