Local loopback address: 127.0.0.1
Simple version of the server:
Import socket Server = socket.socket () # to buy such a phone server.bind (( " 127.0.0.1 " , 8080)) # the bind is bound IP address and port number, note that a tuple, like on the mobile phone card inserted into the phone server.listen (5) # semijoin pool, waiting for the maximum number of connections for five, like on the boot conn, address = server.accept () # to answer the call waiting for someone to give you a telephone DATE = conn.recv (1024) # listening to others, receiving 1023 bytes Print (DATE) conn.send (b " the Hello " ) # for others to answer conn.Close () # hang up the phone server.close () # shutdown
Easy client:
Import socket Client = socket.socket () # holding the phone the client.connect (( " 127.0.0.1 " , 8080)) # binding is the IP address and port number, but also a tuple dial client.send (b " the Hello " ) # others messaging DATE = client.recv (1024) # receiving people talk, no time to receive 1024 bytes Print (DATE) client.close () # on the phone
Note: send and recv need one correspondence at the time of writing the server and client, can no longer appear on both sides, with the recv is a memory to the data, as the source of the data without regard to
Stick package:
Server:
Import socket Server = socket.socket () # to buy such a phone server.bind (( " 127.0.0.1 " , 8088)) # the bind is bound IP address and port number, note that a tuple, like on the mobile phone card inserted into the phone server.listen (5) # semijoin pool, waiting for the maximum number of connections for five, like on the boot conn, address = server.accept () # to answer the call waiting for someone to give you a telephone DATE = conn.recv (1024) # listening to others, receiving 1023 bytes Print (DATE) DATE = conn.recv (1024) # listening to others, receiving 1023 bytes Print (DATE) conn.Close () # hang up the phone server.close () # shutdown
Client:
Import socket Client = socket.socket () # holding the phone the client.connect (( " 127.0.0.1 " , 8088)) # binding is the IP address and port number, but also a tuple dial client.send (b " the Hello " ) # others message client.send (b " the Hello " ) # others messaging client.close () # on the phone
Server print the results:
b ' hellohello '
This is because the tcp agreement would short time interval, and the file will be a great little package sent to the other party
Stick package to solve the problem:
struct module:
Import struct Print ( " -------------------------- 1 ---------------- ---------- " ) MSG = " asdasdasdasdasd " Print ( " the length of the original string " ) Print (len (MSG)) Handler = struct.pack ( " I " , len (MSG)) Print ( " Create header length " ) Print (len (Handler)) RES = struct.unpack ( " I " , Handler) [0] Print ( " length header after the solution " ) Print (RES) Print ( " -------------------------- 2- ------------------------- " ) MSG1 = " asdasdasdasdasdasdasdasd " Print ( " the length of the original string " ) Print (len (MSG1)) handler1 = struct.pack ( " I " , len (MSG1)) Print ( " Create header length " ) Print(len (handler1)) RES1 = struct.unpack ( " I " , handler1) [0] Print ( " length header after the solution " ) Print (RES1) "" " --------- 1 ----------------- -------------------------- length of the string of 15 creating a header length 4 the length of the solution after the header 15 -------------------------- 2 ------------ -------------- length of the string of 24 creates a header of length 4 the length of the solution after the header 24 "" "
We can report the hair in the past, and then extract the header, you can know that the size of the data, if the byte size than we are unacceptably large, we can always let it received until the receive is completed,
Server:
import json import os import socket import struct server_path = r'/Users/mac/Documents/client-server/server_movie' server = socket.socket() ip_port = ('127.0.0.1', 8080) server.bind(ip_port) server.listen(5) conn, addr = server.accept() while 1: head_len = conn.recv(4) head_len = struct.unpack('i', head_len)[0] json_head = conn.recv(head_len).decode('utf-8') head = json.loads(json_head) file_size = head['file_size'] with open(os.path.join(server_path, head['file_name']), 'wb') as f: while file_size: if file_size >= 1024: content = conn.recv(1024) f.write(content) file_size -= 1024 else: content = conn.recv(file_size) f.write(content) break
Client:
import os import socket import json import struct client = socket.socket() ip_port = ('127.0.0.1', 8080) client.connect(ip_port) head = { 'file_path': r'/Users/mac/Documents/client-server/client_movie', 'file_name': None, 'file_size': None } movie_list = os.listdir(head['file_path']) while 1: for i, m in enumerate(movie_list, 1): print('%s --> %s' % (i, m)) choice = input('please input movie number:>>').strip() if choice.isdigit(): choice = int(choice) if choice in range(1, len(movie_list) + 1): movie_name = movie_list[choice - 1] head['file_name'] = movie_name head['file_size'] = os.path.getsize(os.path.join(head['file_path'], head['file_name'])) json_head = json.dumps(head).encode('utf-8') bytes_head = len(json_head) pack_head = struct.pack('i', bytes_head) client.send(pack_head) client.send(json_head) file_size = head['file_size'] with open(os.path.join(head['file_path'], head['file_name']), 'rb') as f: while file_size: if file_size >= 1024: content = f.read(1024) client.send(content) file_size -= 1024 else: content = f.read(file_size) client.send(content) print("finish") break else: print('index out of range') else: print('input number...')