socket server端和client端 簡單備忘

python 版本 2.7python

server 端socket

# -*- coding:utf-8 -*-
import socket

sk = socket.socket()

sk.bind(('127.0.0.1',9999))
sk.listen(5)
while True:
    print('server waiting...')
    conn,address = sk.accept()
    res = str(conn.recv(1024))
    print(res)
    conn.sendall(bytes("hello,world!"))
    conn.close()

client端ide

# -*- coding:utf-8 -*-
import socket

sk = socket.socket()

sk.connect(('127.0.0.1',9999))
sk.sendall(bytes('你大爺!'))
ret = str(sk.recv(1024))
print(ret)
sk.close()
相關文章
相關標籤/搜索