~cpp #---------------------------------------------------------- # 타임서버 v0.1 # #---------------------------------------------------------- import time from socket import * PORT = 13 HOST = '' # localhost를 의미 def timeserver_calculation(): return time.asctime( time.gmtime() ) def daytimeserver(host=HOST, port=PORT, backlog=5): serversock = socket(AF_INET, SOCK_STREAM) serversock.bind( (host,port) ) # 튜플! serversock.listen(backlog) print 'Listening on Port %s (%s, %s)' % (port, 'host', backlog) while 1: conn, addr = serversock.accept() daytime = timeserver_calculation() print 'Connected for %s Client: %s, Port: %s' % (daytime, addr, port) conn.send(daytime) conn.close() if __name__ == '__main__': daytimeserver()
~cpp #---------------------------------------------------------- # 타임클라이언트 v0.1 # import sys from socket import * BUFSIZE = 1024 HOST = 'localhost' #HOST = '203.254.163.74' PORT = 13 def daytimeclient(host=HOST, port=PORT): clientsock = socket(AF_INET, SOCK_STREAM) try: clientsock.connect( (host, port) ) except: print 'connect: Connection retused' return svr_time = clientsock.recv(BUFSIZE) print svr_time clientsock.close() if __name__ == '__main__': daytimeclient()
~cpp import time from socket import * PORT = 13 HOST = 'localhost' # localhost를 의미 def server(host=HOST, port=PORT, backlog=5): serversock = socket(AF_INET, SOCK_STREAM) serversock.bind( (host,port) ) # 튜플! serversock.listen(backlog) print 'Listening on Port %s (%s, %s)' % (port, 'host', backlog) while 1: conn, addr = serversock.accept() print 'Connected for %s Client: %s, Port: %s' % (addr, port) user = cerversock.recv(1024) serversock.send(user) if __name__ == '__main__': server()