1. 程式人生 > >Python之socket

Python之socket

127.0.0.1 where ive clas con CP and code AS

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()

  

Python之socket