我一直在有一個程序,無論何時有人按下輸入鍵,不輸入內容,程序將停止輸入。下面Python 3套接字應用程序停止發送數據
#Client
import socket
from time import sleep
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 9990
s.connect((host, port))
print('connecting')
global ab
try:
ab = input('enter input')
except Exception as eb:
ab = 'hello'
s.sendall(ab.encode())
while True:
global hi
try:
hi = input('enter input')
except Exception as b:
hi = input('enter input')
try:
dataNew = s.recv(4024)
s.sendall(hi.encode())
except Exception as be:
print('try again')
if dataNew:
print(dataNew)
if not dataNew:
print('error')
break
服務器:
#Server
import socket
host = 'localhost'
port = 9990
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except Exception as e:
print("Error creating socket")
try:
s.bind((host, port))
print('Binding socket')
s.listen(10)
print('listening')
except Exception as e:
print('Error creating server')
def mainInput():
while True:
try:
dataNew = c.recv(4024)
c.sendall(dataNew)
if not dataNew:
print('waiting or disconnected')
break
if dataNew:
print(dataNew, a)
except Exception as b:
print('error')
def mainLoop():
while True:
global c
global a
c, a = s.accept()
print('Connected by', a)
mainInput()
mainLoop()
的問題是,進入一個空白的消息打破了計劃。