python socket与socketserver模块

2018/03/06 15:12
阅读数 0

server端

import socket
server=socket.socket()
adre=('127.0.0.1',11000)
server.bind(adre)
server.listen(5)   #最多排队数5
conn,adress= server.accept()
while True:
#    conn,adress= server.accept()   #如果需要排队聊天
#   while True:
    data = conn.recv(1024)
    print(str(data, 'utf8'))
    if str(data, 'utf8') == 'exit':
        break
    send_info=input('>>>')
    conn.sendall(bytes(send_info,'utf8'))
server.close()

client端

import socket
client=socket.socket()
adress=('127.0.0.1',11000)
client.connect(adress)
while True:
    send_info=input('>>>')
    client.sendall(bytes(send_info,'utf8'))
    if send_info=='exit':
        break
    data=client.recv(1024)
    print (str(data,'utf8'))
client.close()

文件传输

server端

import socket
import os
path=os.path.join(os.getcwd(),'ceshi')
server=socket.socket()
adre=('127.0.0.1',11000)
server.bind(adre)
server.listen(5)   #最多排队数5
conn,adress= server.accept()
recv_info=conn.recv(1024)
cmd,file,size=str(recv_info,'utf8').split('|')
path_file=os.path.join(path,file)
size=int(size)
recv_size=0
# f=open(path_file,'ab')  
# while recv_size!=size:
#     data = conn.recv(1024)
#     f.write(data)
#     recv_size+=len(data)
#     print (recv_size)
# f.close()
with open(path_file,'ab') as f:
    while recv_size != size:
        data=conn.recv(1024)
        f.write(data)
        recv_size+=len(data)
        #print (recv_size)
server.close()

client端

import socket
import os
path=os.getcwd()
inp=input('>>>')
#cmd,file=inp.split('|')
path_file=os.path.join(path,inp)
size=os.stat(path_file).st_size
send_info='post|%s|%s' %(inp,size)
client=socket.socket()
adress=('127.0.0.1',11000)
client.connect(adress)
client.sendall(bytes(send_info,'utf8'))
read_size=0
#f=open(path_file,'rb')
# while read_size!=size:
#     read_info=f.read(1024)
#     client.sendall(read_info)
#     read_size+=len(read_info)
#     print (read_size)
# f.close()
with open(path_file,'rb') as f:
    while read_size!=size:
        read_info=f.read(1024)
        client.sendall(read_info)
        read_size+=len(read_info)
        #print (read_size)
client.close()

socketserver 并发聊天

###server端#########
import socketserver
class Server(socketserver.BaseRequestHandler):
    def handle(self):  #父类方法,重写handle方法
        print ('server端启动...')
        while True:
            conn=self.request   #等于conn,adress= server.accept()
            print (self.client_address)
            while True:
                client_data=conn.recv(1024)
                print (str(client_data,'utf8'))
                inp=input('>>>')
                conn.sendall(bytes(inp,'utf8'))
            conn.close()
if __name__=='__main__':
    server=socketserver.ThreadingTCPServer(('127.0.0.1',11000),Server)
    server.serve_forever()
####client端#####
import socket
client=socket.socket()
adress=('127.0.0.1',11000)
client.connect(adress)
while True:
    send_info=input('>>>')
    client.sendall(bytes(send_info,'utf8'))
    if send_info=='exit':
        break
    data=client.recv(1024)
    print (str(data,'utf8'))
client.close()

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部