37,741
社区成员
发帖
与我相关
我的任务
分享
if __name__ == '__main__':
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 2131))
sock.listen(5)
while True:
print 'reday!'
connection,address = sock.accept()
try:
print address
connection.settimeout(5)
buf = connection.recv(1024)
if buf == '1':
print 'welcome to server!'
connection.send('welcome to server!')
else:
print 'please go out!'
connection.send('please go out!')
except socket.timeout:
print 'time out'
connection.close()
if __name__ == '__main__':
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', 2131))
#import time
#time.sleep(2)
print 123
sock.send('1')
print sock.recv(1024)
print 456
sock.close()