Anda di halaman 1dari 3

Server

import socket
import threading
import os
// ^ disini kita mengimport module-module yang diperlukan untuk server socket.
def RetrFile(name, sock):
filename = sock.recv(1024)
if os.path.isfile(filename):
sock.send("EXISTS " + str(os.path.getsize(filename)))
userResponse = sock.recv(1024)
if userResponse[:2] == 'OK':
with open(filename,'rb') as f:
bytesToSend = f.read(1024)
sock.send(bytesToSend)
while bytesToSend != " ":
bytesToSend = f.read(1024)
sock.send(bytesToSend)
else:
sock.send("ERROR")
//^ ini mendefinisikan file dan socket dari server. Pada code if......dst, disini akan dicek apakah
keberadaan file tersebut ada atau tidak. Jika ada (exist) maka akan bisa melakukan download file.
Jka tidak maka akan lompat ke code else: .. maka yang akan muncul sebuah notifikasi ERROR.
sock.close()
//^ ini menutup socket apabila tidak ada file yang dikirim atau sudah muncul ERROR
def Main():
host = '127.0.0.1'
port = 5000
s = socket.socket()
s.bind((host,port))
//^ kami menggunakan local host 127.0.0.1 dengan port 5000 antara server dan client untuk
berkomunikasi
s.listen(5)
print "\t\t\t\t_____Service On Server Berjalan____.\n\t\t\t\t =++|*Welcom To ILKOM
SERVER*|++="
print "\t\t\t\t\t -----(^_^)-----"
print "\t\t\t\t ____ Menunggu Klien____"
while True:
c, addr = s.accept()
print "client connected(!>.<!):<"
t = threading.Thread(target=RetrFile, args=("retrThread", c))
t.start()

s.close()
//^ ini berguna untuk mengecek client. Jika sudah ada client yang terhubung, dan memulai threading
(sambungan) untuk proses download di client.
if __name__ == '__main__':
Main()
//^ ini berfungsi untuk mendefinisikan aktivitas yang terjadi antar klien-server
Client
import socket
//^ ini untuk mengimport module socket yang menghubungkan server client
def Main():
host = '127.0.0.1'
port = 5000
s=socket.socket()
s.connect((host,port))
//^ini berisi host dan port yang sama dengan server, intinya untuk menghubungkan client ke server
filename = raw_input("Request File Name ->= ")
if filename != 'q':
s.send(filename)
data = s.recv(1024)
if data[:6] == 'EXISTS':
filesize = long(data[6:])
message = raw_input("FIle Exists, "+str(filesize)+\
"Bytes, download? (y/n)? -> ")
if message == 'y':
s.send('OK')
f = open(filename, 'wb')
data = s.recv(1024)
totalRecv = len(data)
f.write(data)
while totalRecv
< filesize:
data = s.recv(1024)
totalRecv += len(data)
f.write(data)
print "{0:.2f}".format((totalRecv/float(filesize))*100)+\
"% done"
print "Download Complete"
else :
print "File doesnt exist"
s.close()
//^ ini untuk client meminta file yang akan didownload dari server. Jika file yang dicari ada maka
akan muncul notifikasi file exist beserta size dari file tersebut. Lalu saat klien memulai download,

muncul persentase download progress sampai download selesai (muncul notifikasi download
complete). Jika file yang dicari tidak ada atau download sudah selesai, maka sambungan socket
antara server dan klien akan ditutup.
if __name__ == '__main__':
Main()
//^ ini berfungsi untuk mendefinisikan aktivitas yang terjadi antar klien-server
Screenshot

Anda mungkin juga menyukai