Python之socket
Posted 亚洲哈登
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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()
以上是关于Python之socket的主要内容,如果未能解决你的问题,请参考以下文章