# from Wsgiref.simple_server import Make_server
#
# def Runserver (environ,start_response):
# start_response (status= ' OK ', headers=[(' Content-type ', ' text/html ')])
# url=environ[' Path_info ')
#
# return "Guozhendong"
#
# if __name__== ' __main__ ':
# Httpd=make_server (", 8008,runserver)
# print ("Servering HTTP on port 8008 ....")
# Httpd.serve_forever ()
"""
Python Network programming
Socket (TCP, IP) sockets
Service side
1, run up IP and port, waiting for someone else's connection
Client
Client
Client
The following is the server-side code
"""
Import socket
Sk=socket.socket ()
Sk.bind (("172.8.250.59", 8008))
Sk.listen (5) #表示只能等待5个人
While True:
conn, IP = sk.accept () # Receives the client's request and will block, the program has been waiting for the connection in this sentence, the following statement will not be executed
# conn and IP for getting the IP address and port to the Connection object
#print (conn, IP)
Conn.sendall (bytes (' Hello ', encoding= ' utf-8 '))
While True:
RET_BYTES=CONN.RECV (1024)
Ret_str=str (ret_bytes,encoding= ' utf-8 ')
#print (RET_STR)
Conn.sendall (Bytes (ret_str+ "performed here", encoding= ' Utf-8 '))
"""
This is the service side of socket network programming.
"""
Import socket
Obj=socket.socket ();
Obj.connect (("172.8.250.59", 8008))
RECIVE=OBJ.RECV (1024x768) #最多接收1024字节
Recive=str (recive,encoding= ' utf-8 ')
Print (recive)
While True:
Inp=input ("Please enter what you want to send")
If inp== ' Q ':
Obj.sendall (Bytes (INP, encoding= ' Utf-8 '))
Break
Else
Obj.sendall (Bytes (INP, encoding= ' Utf-8 '))
Print (str (OBJ.RECV (1024x768), encoding= ' Utf-8 '))
------------------------------------------------------------------
Send files using the socket
# from Wsgiref.simple_server import Make_server
#
# def Runserver (environ,start_response):
# start_response (status= ' OK ', headers=[(' Content-type ', ' text/html ')])
# url=environ[' Path_info ')
#
# return "Guozhendong"
#
# if __name__== ' __main__ ':
# Httpd=make_server (", 8008,runserver)
# print ("Servering HTTP on port 8008 ....")
# Httpd.serve_forever ()
"""
Python Network programming
Socket (TCP, IP) sockets
Service side
1, run up IP and port, waiting for someone else's connection
Client
Client
Client
"""
Import socket
Sk=socket.socket ()
Sk.bind (("172.8.250.59", 8008))
Sk.listen (5) #表示只能等待5个人
While True:
conn, IP = sk.accept () # Receives the client's request and will block, the program has been waiting for the connection in this sentence, the following statement will not be executed
# conn and IP for getting the IP address and port to the Connection object
Conn.sendall (Bytes ("Hello, Welcome Landing", encoding= ' Utf-8 '))
F=open (' new.png ', ' WB ')
File_size=str (Conn.recv (1024x768), encoding= ' Utf-8 ') #接收到服务端发送的文件的大小
Conn.sendall (Bytes ("Start Bar", encoding= ' Utf-8 '))
Total_size=int (File_size)
Print (total_size)
Has_rev=0
While True:
If Total_size==has_rev:
Break
RET_BYTES=CONN.RECV (1024)
F.write (Ret_bytes)
Has_rev+=len (Ret_bytes)
F.close ();
#客户端
"""
This is the service side of socket network programming.
"""
Import socket
Import OS
Obj=socket.socket ();
Obj.connect (("172.8.250.59", 8008))
RECIVE=OBJ.RECV (1024x768) #最多接收1024字节
Recive=str (recive,encoding= ' utf-8 ')
Print (recive)
#获取当前文件的大小
Size=os.stat (' f.jpg '). st_size
Print (Size,type (size))
OBJ.RECV (1024x768) #解决粘包问题
Obj.sendall (bytes (str (size), encoding= ' Utf-8 '))
With open (' f.jpg ', ' RB ') as F:
For line in F:
Obj.sendall (line)
Obj.close ()
"""
To automatically stop receiving files after they are sent, you need to get the size of the current file
"""
#由于文件发送依赖缓存区, but the buffer file to send things need to wait a while, and then send the file in the past, if the file and the size of the front and other things put together to send, this will cause the sticky packet, solve the problem of sticky packets need the server side to receive the file size, return a certain message, Can solve the problem of sticky bag
-----------------------------------------------
With Socketserver you can implement multiple connection accesses, the code is as follows
Import Socketserver
Class MyServer (Socketserver. Baserequesthandler):
def handle (self):
Conn=self.request
Conn.sendall (bytes (' Welcome to me '), encoding= ' Utf-8 ')
While True:
RET_BYTES=CONN.RECV (1024)
Ret_str=str (ret_bytes,encoding= ' utf-8 ')
If ret_str== ' Q ':
Break
Conn.sendall (Bytes (ret_str+ ', encoding= ' utf-8 '))
If __name__== ' __main__ ':
Server=socketserver. Threadingtcpserver (("172.8.250.59", 8008), MyServer)
Server.serve_forever ()
Python network programming (Socket)