服务端:

# -*- coding: utf-8 -*-
import socket
import os
import time
#监听ip和端口
IP_Post = ('0.0.0.0', 1234)
sk = socket.socket()
sk.bind(IP_Post)
sk.listen(5)

while True:
    conn, addr = sk.accept()

    # #循环接收文件
    # while True:
    data = conn.recv(1024)
    # print(str(data,'utf-8'))
    print(data)
    file_name, file_size = str(data, 'utf-8').split('|')

    # path=os.path.join(file_name,".jpg")
    path = file_name+".jpg"
    file_size = int(file_size)
    has_sent = 0
    with open(path, 'wb') as fp:
        while has_sent != file_size:
            data = conn.recv(1024)
            fp.write(data)
            has_sent += len(data)
    conn.close()

客户端:

import os
import socket
import cv2
import time


def s(name):
    #服务端ip和端口
    IP_Post=('192.168.1.104',1234)
    sk=socket.socket()
    sk.connect(IP_Post)
    size=os.stat("./1.jpg").st_size
    data=name+"|"+str(size)
    sk.sendall(bytes(data,'utf-8'))

    has_sent=0
    with open("./1.jpg",'rb')as fp:
        while has_sent != size:
            data = fp.read(1024)
            sk.sendall(data)
            has_sent += len(data)

    sk.close()

while True:
    cam=cv2.VideoCapture(0)
    ret,c=cam.read()
    cam.release()
    cv2.imwrite("./1.jpg",c)
    s(str(int(time.time())))
    #延时30秒
    time.sleep(30)
Last modification:October 24th, 2019 at 03:46 pm
如果觉得我的文章对你有用,请随意赞赏