import socket import datetime from local_machine_info import print_machine_info host = socket.gethostname() port = 12345 echo_server = socket.socket() echo_server.bind((host, port)) echo_server.listen(5) print datetime.datetime.now() print "Cekam klijenta..." #conn, addr = echo_server.accept() print "Spojen: " print "Machine_info_print: => ", print_machine_info() while True: conn, addr = echo_server.accept() data = conn.recv(1024) if not data: break if data == 'aspira': conn.sendall('Ne moze.') else: conn.sendall(data)
import threading import time import datetime from local_machine_info import print_machine_info datum = datetime.datetime.now() print(datum) print_machine_info() class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print "Pokrecem nit " + self.name # Ostvari lock zbog sinkronizacije niti threadLock.acquire() print_time(self.name, self.counter, 3) # Oslobodi lock da bi se izvrsila sljedeca nit threadLock.release() def print_time(threadName, counter, delay): while counter: time.sleep(delay) print "%s: %s" % (threadName, time.ctime(time.time()))
import socket import datetime from local_machine_info import print_machine_info adresa_servera = ('localhost', 12345) client_socket = socket.socket() client_socket.connect(adresa_servera) print(print_machine_info()) print(datetime.datetime.now()) print('Molim vas upisite tekst za slanje serveru:') poruka = input(" -> ") while poruka != 'aspira': client_socket.send(poruka.encode()) data = client_socket.recv(1024).decode() print('Odgovor servera: ' + data.decode()) client_socket.close()