作者:so的青春 | 来源:互联网 | 2023-07-21 16:49
在win10上面,python2.7,我有一个最简单的服务器端:fromSocketServerimport(TCPServerasTCP,StreamRequestHandler
在win10上面,python2.7,我有一个最简单的服务器端:
from SocketServer import (TCPServer as TCP, StreamRequestHandler as SRH)
from time import ctime
HOST=''
PORT=21571
ADDR=(HOST,PORT)
class MyRequestHandler(SRH):
def handler(self):
print '...connected from :', self.client_address
self.wfile.write('[%s] %s' % (ctime(), self.rfile.readline()))
tcpServer=TCP(ADDR,MyRequestHandler)
tcpServer.serve_forever()
然后我写了一个简单的客户端来连接和发数据:
from socket import socket, AF_INET, SOCK_STREAM
HOST='localhost'
PORT=21571
BUFSIZ=1024
ADDR=(HOST,PORT)
while True:
tcpCliSock=socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data=raw_input('> ')
if not data:
break
tcpCliSock.send('%s\r\n' % data)
data=tcpCliSock.recv(BUFSIZ)
if not data:
break
print data.strip()
tcpCliSock.close()
可是它运行到recv这一行的时候就会报错:
python clientSocket01.py
> sdfa
Traceback (most recent call last):
File "clientSocket01.py", line 16, in
data=tcpCliSock.recv(BUFSIZ)
socket.error: [Errno 10053]
看半天没看出来我的程序哪里有问题。还请大侠帮忙!
3 个解决方案
10053,你这个对应的端口等能不能访问,服务器端有没有侦听