Python 下的 tcp server/client 通信

版权声明:本文为大事龙原创文章,未经允许不得转载。 https://blog.csdn.net/w_yunlong/article/details/78907991

说明:只做基础,不做延伸,直接上代码

1、源文件

  • server.py
from socket import *

server = socket(AF_INET, SOCK_STREAM)
server.bind(("localhost", 5555))
server.listen(1)

sock, addr = server.accept()
print "[+] connected from ", addr
sock.send('Hello')

while True:

    r = sock.recv(1024)

    print "[+] message:", r

    sock.send("you said: " + r)

server.close()
  • client.py
from socket import *

client = socket(AF_INET, SOCK_STREAM)
client.connect(("localhost", 5555))

while True:
    r = client.recv(1024)
    print "[+] recv data", r

    data = raw_input("input ur words:")

    client.send(data)

2、运行

打开终端并执行 server:python server.py
打开终端并执行 client:python client.py

猜你喜欢

转载自blog.csdn.net/w_yunlong/article/details/78907991