Python之socket

1.1socket编程之tcp编程

"""
socket类型
sock_stream 面向连接的流套接字,默认值 tcp协议
sock_dgram  无连接的数据报文套接字,udp协议
"""
import socket
s = socket.socket()
s.bind(('127.0.0.1',9999))  #bind接受一个2元祖
s.listen()
"""
Accept a connection. The socket must be bound to an address and listening for connections.
The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, 
and address is the address bound to the socket on the other end of the connection
"""
new_socker,info = s.accept() #只接受一个client请求,阻塞
data=new_socker.recv(1024)  #阻塞
print(data)
print(type(data))
new_socker.send('back {}'.format(data).encode())
s.close()

  

猜你喜欢

转载自www.cnblogs.com/harden13/p/9189869.html