| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2636 人关注过本帖
标题:PYTHON网络编程
只看楼主 加入收藏
qscf_520
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-7-9
收藏
 问题点数:0 回复次数:0 
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
搜索更多相关主题的帖子: 网络编程 用户名 服务器 listen import 
2015-07-09 11:20
快速回复:PYTHON网络编程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016055 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved