Python socket

 

 

 

 

Simple example of the first part of the socket

Server part:

"""
Description:
Author:Nod
Date:
Record:
#---------------------------------v1-----------------------------------#
"""


import socket

# socket.AF_INET specifies the type of socket 
# SOCK_STREAM specifies the streaming media protocol of tcp 
phone= socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Bind port and address 
phone.bind(( ' 127.0.0.1 ' ,8080 ))

#The maximum number of connections to monitor is mainly for concurrency problems 
phone.listen(5 )


#
print('wating for =====')
conn,client_addr=phone.accept()
print(conn,client_addr)


data =conn.recv(1024) #Receive     message print 
( ' \ 033[31;1m%s\033[0m ' % data)
conn.send(data.upper()) #Send a    message conn.close 

()

Client part: note that when sending data it is sent in binary

"""
Description:
Author:Nod
Date:
Record:
#---------------------------------v1-----------------------------------#
"""



import socket
plone = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

plone.connect(('127.0.0.1',8080))


plone.send('hello'.encode('utf-8'))

data=plone.recv(1024)
print(data)

plone.close()
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324820793&siteId=291194637