文章会有【原创】或者【转载】标示,部分来自Google Baidu的学习结果 {Java/PHP/Python/Ruby/Go}

Python3 TCP Socket编程【原创】  


#-*- coding: UTF-8 -*-
#使用socket模块(TCP)
import socket
def tcpServer():
    address = ('127.0.0.1', 31500)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s = socket.socket()
    s.bind(address)
    s.listen(5)
    ss, addr = s.accept()
    print("Welcome to 127.0.0.1")
    print('got connected from',addr)
    ss.send(b'byebye')
    ra = ss.recv(512)
    print(ra)
    ss.close()
    s.close()

if __name__ == "__main__":
    while True:
        tcpServer()
#-*- coding: UTF-8 -*-
#使用socket模块(TCP)
import socket
def tcpClient():
    address = ('127.0.0.1', 31500)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(address)
    data = s.recv(512)
    print('the data received is',data)
    s.send(b'hihi')
    s.close()

if __name__ == "__main__":
    tcpClient()