如果未连接,如何退出服务器连接
how to exit from the server connection if not connected
我有两台服务器,如果服务器 1 无法连接,客户端程序等待并且服务器 2 未获取数据,我的客户端希望向两者发送相同的数据 server.but。我想等server1 1s,如果连接失败,server2 会获取数据。
import socket
s1 = socket.socket()
s2 = socket.socket()
host1 = '192.168.0.3'
port1 = 12345
host2 = '192.168.0.5'
port2=12321
s1.connect((host1, port1))
s1.send(data)
s2.connect((host2,port2))
s2.send(data)
s1.close()
s2.close()
我认为如果您无法连接到服务器,它会自动抛出异常,如果您捕获到异常,将执行以下行。
例如:-
客户端
import socket
import struct, time
import sys
# server
HOST = "localhost"
PORT = 13
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00
# connect to server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((HOST, PORT))
except: # catch *all* exceptions
e = sys.exc_info()[0]
print(e)
try:
s2.connect((HOST, 8037))
except: # catch *all* exceptions
e = sys.exc_info()[0]
# read 4 bytes, and convert to time value
t = s2.recv(4)
t = struct.unpack("!I", t)[0]
t = int(t - TIME1970)
s.close()
# print results
print "server time is", time.ctime(t)
print "local clock is", int(time.time()) - t, "seconds off"
服务器端
import socket
import struct, time
# user-accessible port
PORT = 8037
# reference time
TIME1970 = 2208988800L
# establish server
service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
service.bind(("", PORT))
service.listen(1)
print "listening on port", PORT
while 1:
# serve forever
channel, info = service.accept()
print "connection from", info
t = int(time.time()) + TIME1970
t = struct.pack("!I", t)
channel.send(t) # send timestamp
channel.close() # disconnect
在上面的客户端代码中,一个服务器端口不退出,即 localhost:13,所以它会抛出异常,我捕获了异常,然后执行了错误代码之后的代码,因此连接到服务器 localhost:8037,它返回了数据.
简单加试试
try:
s1.connect((host1, port1))
s1.send(data)
except:
print " s1 not connected"
try:
s2.connect((host2,port2))
s2.send(data)
except:
print"s2 not connected"
s1.close()
s2.close()
我有两台服务器,如果服务器 1 无法连接,客户端程序等待并且服务器 2 未获取数据,我的客户端希望向两者发送相同的数据 server.but。我想等server1 1s,如果连接失败,server2 会获取数据。
import socket
s1 = socket.socket()
s2 = socket.socket()
host1 = '192.168.0.3'
port1 = 12345
host2 = '192.168.0.5'
port2=12321
s1.connect((host1, port1))
s1.send(data)
s2.connect((host2,port2))
s2.send(data)
s1.close()
s2.close()
我认为如果您无法连接到服务器,它会自动抛出异常,如果您捕获到异常,将执行以下行。
例如:-
客户端
import socket
import struct, time
import sys
# server
HOST = "localhost"
PORT = 13
# reference time (in seconds since 1900-01-01 00:00:00)
TIME1970 = 2208988800L # 1970-01-01 00:00:00
# connect to server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((HOST, PORT))
except: # catch *all* exceptions
e = sys.exc_info()[0]
print(e)
try:
s2.connect((HOST, 8037))
except: # catch *all* exceptions
e = sys.exc_info()[0]
# read 4 bytes, and convert to time value
t = s2.recv(4)
t = struct.unpack("!I", t)[0]
t = int(t - TIME1970)
s.close()
# print results
print "server time is", time.ctime(t)
print "local clock is", int(time.time()) - t, "seconds off"
服务器端
import socket
import struct, time
# user-accessible port
PORT = 8037
# reference time
TIME1970 = 2208988800L
# establish server
service = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
service.bind(("", PORT))
service.listen(1)
print "listening on port", PORT
while 1:
# serve forever
channel, info = service.accept()
print "connection from", info
t = int(time.time()) + TIME1970
t = struct.pack("!I", t)
channel.send(t) # send timestamp
channel.close() # disconnect
在上面的客户端代码中,一个服务器端口不退出,即 localhost:13,所以它会抛出异常,我捕获了异常,然后执行了错误代码之后的代码,因此连接到服务器 localhost:8037,它返回了数据.
简单加试试
try:
s1.connect((host1, port1))
s1.send(data)
except:
print " s1 not connected"
try:
s2.connect((host2,port2))
s2.send(data)
except:
print"s2 not connected"
s1.close()
s2.close()