1 Star 0 Fork 0

RTsien / Code Fragments

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
网络编程.py 976 Bytes
一键复制 编辑 原始数据 按行查看 历史
RTsien 提交于 2014-08-26 14:38 . new file
#客户端
if __name__ == '__main__':
import socket
import os
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 8001))
import time
time.sleep(2)
sock.send('1')
print sock.recv(1024)
#os.system('pause')
#raw_input()
time.sleep(3)
sock.close()
#服务端
if __name__ == '__main__':
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8001))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf == '1':
connection.send('welcome to server!')
else:
connection.send('please go out!')
except socket.timeout:
print 'time out'
connection.close()
C
1
https://gitee.com/rtsien/Code-Fragments.git
git@gitee.com:rtsien/Code-Fragments.git
rtsien
Code-Fragments
Code Fragments
master

搜索帮助