python socket编程客户端不输入任何内容,直接回车,服务端收不到任何消息
在编写socket时碰到一个问题,客户端使用input使用户输入内容,当什么都不输入,直接回车,此时服务端并不能收到任何的消息,不知道是什么原因?麻烦大家帮忙看看。
server.py
```python
import socket
IP_PORT=('localhost',6000)
while 1:
ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ss.bind(IP_PORT)
ss.listen(2)
print('listening...')
conn,addr=ss.accept()
print('connected by %s' %(addr,))
while 1:
try:
conn.settimeout(10)
client_data=conn.recv(1024)
if client_data.decode('utf-8')=='1':
conn.send(b'%s %s' %('您输入的是option是'.encode('utf-8'),client_data))
elif client_data.decode('utf-8')=='2':
conn.send(b'%s %s' %('您输入的是option是'.encode('utf-8'),client_data))
elif client_data.decode('utf-8')=='bye':
conn.send(b'%s %s \n%s' %('您输入的是option是'.encode('utf-8'),client_data,'BYE! see you next time'.encode('utf-8')))
conn.close()
break
elif client_data.decode('utf-8')=='':
print(9999999999999999)
conn.send('请输入内容'.encode('utf-8'))
else:
print(client_data.decode('utf-8').upper())
conn.send('received'.encode('utf-8'))
except socket.timeout:
print('timeout...disconnect...')
conn.close()
break
```
client.py
```python
import socket
IP_PORT=('localhost',6000)
cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cs.connect(IP_PORT)
while 1:
user_data=input('Please input>')
cs.send(user_data.encode('utf-8'))
server_info=cs.recv(1024)
print('server_info',server_info.decode('utf-8'))
if user_data=='bye':
cs.close()
break
```