这个文章的系列很有意思,练练~~:
http://python.jobbole.com/82763/
:)
第一步,最简单的SERVER和CLIENT。感觉和写JAVA,C的最简单的一样一样的,,看来,我只会最简单的?:)
server.py及client.py
#Pserver import socket HOST = ‘‘ PORT = 8088 reply = ‘Yes‘ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) print ‘Pserver start listen...‘ s.listen(3) conn, addr = s.accept() request = conn.recv(1024) print ‘request is: ‘, request print ‘Connect by ‘, addr conn.sendall(reply) conn.close()
#Pclient import socket HOST = ‘127.0.0.1‘ PORT = 8088 request = ‘cat you hear me?‘ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) s.sendall(request) reply = s.recv(1024) print ‘reply is: ‘, reply s.close()
时间: 2024-12-14 02:57:19