59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
|
#!coding=utf-8
|
||
|
import os
|
||
|
import socket
|
||
|
import struct
|
||
|
import threading
|
||
|
|
||
|
|
||
|
def Video_reception(ip='0.0.0.0', port=8025):
|
||
|
ADDR = (ip, port)
|
||
|
try:
|
||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||
|
s.bind(ADDR) # 绑定ip
|
||
|
s.listen(4)
|
||
|
except socket.error as msg:
|
||
|
print(msg)
|
||
|
os.sys.exit(1)
|
||
|
# print("等待连接...")
|
||
|
while 1:
|
||
|
client, addr = s.accept()
|
||
|
data = threading.Thread(target=deal_data, args=(client, addr))
|
||
|
data.start()
|
||
|
|
||
|
|
||
|
def deal_data(client, addr):
|
||
|
# print('客户端地址为: {0}'.format(addr))
|
||
|
client.send('hi, Welcome to the server!'.encode('utf-8'))
|
||
|
|
||
|
fileinfo_size = struct.calcsize('128sl')
|
||
|
buf = client.recv(fileinfo_size)
|
||
|
if buf:
|
||
|
# 获取文件名和文件大小
|
||
|
filename, filesize = struct.unpack('128sl', buf)
|
||
|
fn = filename.strip(b'\00')
|
||
|
fn = fn.decode(encoding='gbk')
|
||
|
# print('数据名为 {0}, 数据大小为 {1}'.format(str(fn), filesize))
|
||
|
|
||
|
recvd_size = 0 # 定义已接收文件的大小
|
||
|
# 存储在该脚本所在目录下面
|
||
|
fp = open('./video' + '/' + str(fn), 'wb')
|
||
|
# print('开始接收客户端数据...')
|
||
|
|
||
|
while not recvd_size == filesize:
|
||
|
if filesize - recvd_size > 1028:
|
||
|
data = client.recv(1028)
|
||
|
recvd_size += len(data)
|
||
|
else:
|
||
|
data = client.recv(filesize - recvd_size)
|
||
|
recvd_size = filesize
|
||
|
fp.write(data)
|
||
|
fp.close()
|
||
|
# print('{0}数据接收完成...'.format(str(fn)))
|
||
|
client.send('{0}数据接收完成...'.format(str(fn)).encode('utf-8'))
|
||
|
client.close()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
Video_reception()
|