python uses socket to realize image transmission function
- 2021-11-13 02:10:44
- OfStack
In python, socket is used for image transmission between linux server and win10 host for your reference. The specific contents are as follows
Premise: The server and host need to be in the same LAN
Usage:
(1) Save the following two pieces of code in the corresponding location respectively
(2) Run the server-side code first and display Wait for Connection..................
(3) After running the client code, display input the file:
(4) Copy the image address to be transmitted after the output of (3)
1. Server side (mine is an Linux server)
# Server side server.py
import socket
import os
import sys
import struct
def socket_service_image():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# s.bind(('127.0.0.1', 8001))
s.bind((' Change to your server ip Address ', 8001))
s.listen(10)
except socket.error as msg:
print(msg)
sys.exit(1)
print("Wait for Connection.....................")
while True:
sock, addr = s.accept() # addr Yes 1 Tuples (ip,port)
deal_image(sock, addr)
def deal_image(sock, addr):
print("Accept connection from {0}".format(addr)) # View the sender's ip And ports
while True:
fileinfo_size = struct.calcsize('128sq')
buf = sock.recv(fileinfo_size) # Receive picture name
if buf:
filename, filesize = struct.unpack('128sq', buf)
fn = filename.decode().strip('\x00')
new_filename = os.path.join(r'G:\reseive_images\\' + fn) # Create a new picture name on the server side (you can use the original one directly without creating it, as long as the client and the server are not the same 1 The system or the received picture and the original picture are not present 1 Folders)
recvd_size = 0
fp = open(new_filename, 'wb')
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = sock.recv(1024)
recvd_size += len(data)
else:
data = sock.recv(1024)
recvd_size = filesize
fp.write(data) # Write picture data
fp.close()
sock.close()
break
if __name__ == '__main__':
socket_service_image()
2. Client (mine is the win10 host)
# Client client.py
import socket
import os
import sys
import struct
def sock_client_image():
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((' Change to a server ip Address ', 8001)) # Used when the server and client are on different systems or different hosts ip And port, first look at the system network card where the server is located ip
# s.connect(('127.0.0.1', 8001)) # Both the server and the client are in 1 Used under the system ip And ports
except socket.error as msg:
print(msg)
print(sys.exit(1))
filepath = input('input the file: ') # Enter the name of the picture in the current directory xxx.jpg
fhead = struct.pack(b'128sq', bytes(os.path.basename(filepath), encoding='utf-8'),
os.stat(filepath).st_size) # Will xxx.jpg With 128sq Format packaging of
s.send(fhead)
fp = open(filepath, 'rb') # Open the picture you want to transfer
while True:
data = fp.read(1024) # Read in picture data
if not data:
print('{0} send over...'.format(filepath))
break
s.send(data) # With 2 Send picture data in binary format
s.close()
# break # Cyclic transmission
if __name__ == '__main__':
sock_client_image()