客户端与服务端发送数据

client端

 1 import socket
 2 import os
 3 import sys
 4 import struct
 5 #import cv2
 6 #import tkinter as tk
 7 from tkinter import filedialog
 8 from picamera import PiCamera
 9 from time import sleep
10 
11 #capture = cv2.VideoCapture(0)
12 def take_photo():
13     camera = PiCamera()
14     camera.start_preview()
15     camera.capture("./image0.jpg")
16     camera.stop_preview()
17 
18 def take_video():
19     camera = PiCamera()
20     camera.start_preview()
21     camera.start_recording("./video.h264")
22     sleep(10)
23     camera.stop_recording()
24     camera.stop_preview()
25 
26 def socket_client(a):
27     try:
28         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29         s.connect(('192.168.43.43', 6666))  # 服务器的ip及设置端口号
30     except socket.error as msg:
31         print(msg)
32         sys.exit(1)
33 
34     print(s.recv(1024).decode("utf-8"))
35 
36         #root = tk.Tk()
37         #root.withdraw()
38     if a == 1:
39         filepath = "./image0.jpg"
40     elif a == 2:
41         filepath = "./video.h264"
42     print(filepath)
43     if os.path.isfile(filepath):
44             # 定义定义文件信息。128s表示文件名为128bytes长,l表示一个int或log文件类型,在此为文件大小
45         fileinfo_size = struct.calcsize('128sl')
46             # 定义文件头信息,包含文件名和文件大小
47         fhead = struct.pack(
48                 '128sl',
49                 os.path.basename(filepath).encode(encoding="utf-8"),
50                 os.stat(filepath).st_size
51             )
52         print('client filepath: {0}'.format(filepath))
53         s.send(fhead)
54 
55         fp = open(filepath, 'rb')
56         while 1:
57             data = fp.read(1024)
58             if not data:
59                 print('{0} file send over...'.format(filepath))
60                 break
61             s.send(data)
62         os.remove(filepath)
63         
64 
65         print(s.recv(1024).decode("utf-8"))
66         print(s.recv(1024).decode("utf-8"))
67         s.close()
68         
69 
70 
71 if __name__ == '__main__':
72     while True:
73         userinput = int(input("请输入你想要的功能 1 拍照传输图片  2 摄像传输视频:"))
74         if userinput == 1:
75             take_photo()
76             socket_client(userinput)
77         elif userinput == 2:
78             take_video()
79             socket_client(userinput)
80         else:
81             print("未知错误,重新输入")
82             userinput = int(input("请输入你想要的功能 1 拍照传输图片  2 摄像传输视频:"))

serve端

 1 import socket
 2 import threading
 3 import sys
 4 import os
 5 import struct
 6 
 7 
 8 def socket_service():
 9     try:
10         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
12         s.bind(('192.168.43.43', 6666))  # 服务器的ip及设置端口号
13         s.listen(10)# 10 表示  限制服务端同一时刻所能接受客户端连接请求的个数。
14     except socket.error as msg:
15         print(msg)
16         sys.exit(1)
17     print('Waiting connection...')
18 
19     while 1:
20         conn, addr = s.accept()
21         # 返回值是一个新的套接字描述符,它代表的是和客户端的新的连接,
22         # 可以把它理解成是一个客户端的socket, 这个socket包含的是客户端的ip和port信息 。
23         # (当然这个new_socket会从sockfd中继承服务器的ip和port信息,两种都有了),
24         # 而参数中的SOCKET s包含的是服务器的ip和port信息 。
25 
26 
27         t = threading.Thread(target=deal_data, args=(conn, addr))
28         t.start()
29 
30 
31 def deal_data(conn, addr):
32     print('Accept new connection from {0}'.format(addr))
33     print(conn)
34     conn.send('Hi, Welcome to the server!'.encode("utf-8"))
35 
36     while 1:
37         fileinfo_size = struct.calcsize('128sl')
38         buf = conn.recv(fileinfo_size)
39         if buf:
40             filename, filesize = struct.unpack('128sl', buf)
41             fn = filename.strip(b"\x00").decode("utf-8") #将文件名中的  图片名字 提取出来
42             #strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
43             new_filename = os.path.join('./', 'new_' + fn)
44             print(new_filename, filesize)
45             print('file new name is {0}, filesize if {1}'.format(new_filename, filesize))
46 
47             recvd_size = 0  # 定义已接收文件的大小
48             fp = open(new_filename, 'wb')
49             print('start receiving...')
50 
51             while not recvd_size == filesize:
52                 if filesize - recvd_size > 1024:
53                     data = conn.recv(1024)
54                     recvd_size += len(data)
55                 else:
56                     data = conn.recv(filesize - recvd_size)
57                     recvd_size = filesize
58                 fp.write(data)
59             fp.close()
60             print('end receive...')
61         conn.send('已发送'.encode("utf-8"))
62         print(conn.recv(1024).decode('utf-8'))
63         conn.close()
64         break
65 
66 
67 if __name__ == '__main__':
68     socket_service()

猜你喜欢

转载自www.cnblogs.com/liuming-nimi/p/11917775.html