python实现建立tcp通信

实现代码如下:

#tcp协议通信
import socket
class TcpConnect:
    def get_tcp(self):
        #实例化一个基于tcp的socket对象
        mysocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        #建立与localhost:554端口的tcp连接
        mysocket.connect(("localhost",554))
        #发送的内容
        message='hello world'
        #调用发送的方法,并对信息进行编码
        mysocket.send(message.encode('gb2312'))
        #关闭socket连接
        mysocket.close()
if __name__ == '__main__':
    TcpConnect().get_tcp()

猜你喜欢

转载自www.cnblogs.com/badbadboyyx/p/11978894.html