Networking Basics: socket module

socket:

  Socket (Socket) is an abstraction layer, the application can send or receive data through it, can be the same as file open, close, and write operations. Sockets allow an application I / O into the network, and communicate with other applications in the network. Network socket is a combination of IP addresses and ports.

Socket TCP-based protocol: tcp is based on the link, you must first start the server, and then start the client to the server links

server server:

# Server server 
Import socket 
SK = socket.socket () # create a service socket 
sk.bind (( ' 127.0.0.1 ' , 8080))   # to bind to a socket address 
sk.listen ()              # establish listening link, can function to accept the requirement for inspection 
Conn, addr = sk.accept () # receiving client link, receiving values of the object and the client socket address information 
ret = conn.recv (1024) .decode ( ' UTF-. 8 ' )    # receiving the client message 
Print (RET)               # print client message 
conn.send (bytes ( ' Hello ' , encoding = ' UTF-. 8 ' ))# Using the received client sends a message to the client socket 
conn.Close ()             # close client socket 
sk.close ()               # close the server socket (optional)

client client:

Import socket 
SK = socket.socket ()             # create client socket 
sk.connect (( ' 127.0.0.1 ' , 8080))   # attempt to connect to the server 
sk.send (bytes ( ' Nice to meet you ' , encoding = ' UTF -8 ' )) # send message 
RET = sk.recv (1024) .decode ( ' UTF-. 8 ' )      # receiving message 
Print (RET)                       # Print message 
sk.close ()                       # close the client socket

Note: Accept the server receives a connection request of a client, the client returns a new socket object, and an address information, a new socket for the UE to receive or transmit messages. Binding address with the server to bind a socket, and connect the client with the server.

 

Test 1:

# Pretending chat 
# Server: 
Import Socket 
SK = socket.socket () 
sk.bind (( ' 127.0.0.1 ' , 8081 )) 
sk.listen () 
Conn, addr = sk.accept ()
 the while True: 
    RET = Conn .recv (1024) .decode ( ' UTF-. 8 ' )
     Print (RET)
     IF RET == ' Cool: BYE ' : 
        conn.send (bytes ( ' You You grapefruit Cech trouble: BYE ' , encoding = ' UTF- 8 ' ))
        break
    info = input('柚柚柚切克闹:')
    conn.send(bytes('柚柚柚切克闹:%s'%(info),encoding='utf-8'))
conn.close()
sk.close()


#client:
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',8081))
while True:
    chat = input('cool:')
    sk.send(bytes('cool:%s'%(chat),encoding='utf-8'))
    ret = sk.recv(1024).decode('utf-8')
    print(ret)
    if ret == '柚柚柚切克闹:bye':
        sk.send(bytes('cool:bye',encoding='utf-8'))
        break

sk.close()

Test 2:

  Every ten seconds the UE sends a timestamp, a format returns to the client the server after the server receives the time

#server:
import socket,time
sk = socket.socket()
sk.bind(('127.0.0.1',8081))
sk.listen()
conn,addr = sk.accept()
while True:
    ret = conn.recv(1024).decode('utf-8')
    print(ret)
    info_ret = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(ret))) #需要把字符串强转为整型
    conn.send(bytes(str(info_ret),encoding='utf-8'))
conn.Close () 
sk.close () 

# Client 
Import Socket, Time 
SK = socket.socket () 
sk.connect (( ' 127.0.0.1 ' , 8081 ))
 the while True: 
    the time.sleep ( 10 ) 
    Timer = int ( the time.time ()) # stamp value returned float, to be converted into an integer 
    sk.send (bytes (STR (Timer), encoding = ' UTF-. 8 ' )) # can only pass bytes type of the transmitted message is a string 
    RET = sk.recv (1024) .decode ( ' UTF-. 8 ' )
     Print (RET) 

sk.close ()

 Note: transmission of a message is a string note convert between data types

 

Guess you like

Origin www.cnblogs.com/aizhinong/p/11517593.html