PYTHON网络编程
今天测试PYTHON网络编程设置BUFFER值为100
结果打印出来的是整个BUFFER里面的上一次加这一次的内容
有个问题:PYTHON适合做网络编程吗?感觉除了WEB,网络通信介绍的比较少比较简单。
我想做一个客户端用户名+密码发送内容,服务器端收到后判断是否登陆是否允许,请帮忙修改下,谢谢。
import socket,sys,time
HOST =''
PORT =50000
BUFFER =50
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(0)
print('tcpServer listen at: %s:%s\n\r' %(HOST,PORT))
while True:
client_sock,client_addr=sock.accept()
print('%s:%s connect' %client_addr)
while True:
time.sleep(2)
recv=sock.flush() ==》想清理缓存区,以便别读取上一次发送的内容,但是失败
recv = client_sock.recv(BUFFER)
if not recv:
client_sock.close()
# break
print('[Client %s:%s said]:%s' % (client_addr[0],client_addr[1],recv))
time.sleep(2)
client_sock.send('tcpServer has received your message\n')
sock.close()
客户端
import socket,sys,time
HOST='127.0.0.1'
PORT=50000
BUFFER=39
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((HOST,PORT))
while True:
sock.sendall("user00100000000000000000000000000000000000\n"
time.sleep(2)
sock.sendall("passwd\n"
time.sleep(2)
sock.sendall("out\n"
time.sleep(5)
recv=sock.recv(BUFFER)
print('[tcpServer said]: %s' % recv)
sock.close()
结果是:
[Client 127.0.0.1:52420 said]:user00100000000000000000000000000000000000
[Client 127.0.0.1:52420 said]:passwd
[Client 127.0.0.1:52420 said]ut
[Client 127.0.0.1:52420 said]:user00100000000000000000000000000000000000
passwd
[Client 127.0.0.1:52420 said]ut
[Client 127.0.0.1:52420 said]:user00100000000000000000000000000000000000
[Client 127.0.0.1:52420 said]:passwd
out
[Client 127.0.0.1:52420 said]:user00100000000000000000000000000000000000
[Client 127.0.0.1:52420 said]:passwd
out
[Client 127.0.0.1:52420 said]:user00100000000000000000000000000000000000
[Client 127.0.0.1:52420 said]:passwd
[Client 127.0.0.1:52420 said]ut
[root@localhost daima]$ ./client.py
[tcpServer said]: tcpServer has received your message
tcp
[tcpServer said]: Server has received your message
tcpSer
[tcpServer said]: ver has received your message
tcpServer
[tcpServer said]: has received your message
tcpServer ha