Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
yulimi committed Apr 21, 2017
1 parent e610c0f commit a29052e
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 3 deletions.
3 changes: 3 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ def create_sock() :

if str(hasher.hexdigest()) == hash :
print "전송완료"
print "원본 파일 크기 : " + str(original_file_size) + " bytes\t받은 파일 크기 : " + str(recv_file_size) + " bytes"
print "원본 파일 해시값 : " + str(hash) + " 받은 파일 해시값 : " + str(hasher.hexdigest())
else :
print "파일의 해시값이 다릅니다."
print "원본 파일 해시값 : " + str(hash) + " 받은 파일 해시값 : " + str(hasher.hexdigest())
print "원본 파일 크기 : " + str(original_file_size) + " bytes\t받은 파일 크기 : " + str(recv_file_size) + " bytes"
while(True) :
print "파일을 지우시겠습니까?(Y/N) : ",
Expand Down
2 changes: 2 additions & 0 deletions data_crawling.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def search_url(artist, title):

def download_music(music_url, rank, artist, title, collection) :

# re.sub(pattern, string) pattern : 1) \s - 모든 공백을 지움

rank = re.sub('\s', '', rank.strip())
artist = re.sub('\s', '_', artist.strip())
title = re.sub('\s', '_', title.strip())
Expand Down
5 changes: 2 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_tcp_socket() :
# 소켓에 ADDR변수의 주소를 할당해줌
sock.bind(ADDR)
except error as msg :
print 'bind 실패 Error Code : ',; print str(msg[0]); print "Message "; print str(msg[1])
print 'bind 실패 Error Code : ',; print str(msg[0]),; print "Message ",; print str(msg[1])
sys.exit()

try :
Expand All @@ -37,12 +37,11 @@ def create_tcp_socket() :
if __name__ == "__main__" :

HOST = ""
PORT = 5001
PORT = 6001
ADDR = (HOST, PORT)
LISTEN_NUMBER = 15

tcpSock = create_tcp_socket()
count = 0
while (True) :
try :
print '연결 대기중...'
Expand Down
Binary file modified server_thread.pyc
Binary file not shown.
Binary file removed temp.mp3
Binary file not shown.
73 changes: 73 additions & 0 deletions udp_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#coding: utf-8
from udp_functions import *
import hashlib


if __name__ == "__main__" :

HOST = ''
PORT = 6010
ADDR = (HOST, PORT)
BUFSIZE = 1024 * 10


SERVER_HOST = '210.117.182.122'
SERVER_PORT = 6009
SERVER_ADDR = (SERVER_HOST, SERVER_PORT)

csock = create_udp_socket(ADDR)

print "이름을 입력해주세요 : ",
MYNAME = raw_input()
send_message(csock, SERVER_ADDR, MYNAME)

reply, addr = receive_message(csock, BUFSIZE) # server로 부터 접속 응답 받음
print reply

music_list, addr = receive_message(csock, BUFSIZE)
print music_list
print "번호를 입력 해주세요 : ",
number = raw_input()
send_message(csock, SERVER_ADDR, number)

print("전송 받는중...")

out_file = open("temp.mp3", "wb")
while True:
data, addr = receive_message(csock, BUFSIZE)
if data == "EOF": break
send_message(csock, SERVER_ADDR, "okay")
out_file.write(data)
out_file.close()

original_file_size, server_addr = receive_message(csock, BUFSIZE)
hash, server_addr = receive_message(csock, BUFSIZE)

hasher = hashlib.sha224()
with open("temp.mp3", "rb") as f:
buf = f.read()
hasher.update(buf)

recv_file_size = os.path.getsize("temp.mp3")

if str(hasher.hexdigest()) == hash :
print "전송완료"
print "원본 파일 크기 : " + str(original_file_size) + " bytes\t받은 파일 크기 : " + str(recv_file_size) + " bytes"
print "원본 파일 해시값 : " + str(hash) + " 받은 파일 해시값 : " + str(hasher.hexdigest())
else :
print "파일의 해시값이 다릅니다."
print "원본 파일 해시값 : " + str(hash) + " 받은 파일 해시값 : " + str(hasher.hexdigest())
print "원본 파일 크기 : " + str(original_file_size) + " bytes\t받은 파일 크기 : " + str(recv_file_size) + " bytes"
while (True) :
print "파일을 지우시겠습니까?(Y/N) : ",
user_input = raw_input()
if user_input.upper() != 'Y' and user_input.upper() != 'N':
print "잘못 입력 하셨습니다.\n"
continue

elif user_input.upper() == 'Y':
os.remove("temp.mp3")
print("파일 삭제 완료")
break

print("완료")
35 changes: 35 additions & 0 deletions udp_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#coding: utf-8
from socket import *
import os

def create_udp_socket(ADDR) :
try :
sock = socket(AF_INET, SOCK_DGRAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
#print('Socket이 생성되었습니다.')
except error as msg :
print '소켓 생성 실패. Error Code : ',; print str(msg[0]),; print "Message ",; print str(msg[1])
sys.exit()

try :
sock.bind(ADDR)
except error as msg :
print 'bind 실패 Error Code : ',; print str(msg[0]); print "Message ",; print str(msg[1])
sys.exit()

return sock

def receive_message(sock, size) :
data, addr = sock.recvfrom(size)
try :
data = data.decode('utf-8')
except :
pass
return data, addr

def send_message(sock, addr, message) :
try :
message = message.encode('utf-8')
sock.sendto(message, addr)
except :
sock.sendto(message, addr)
Binary file added udp_functions.pyc
Binary file not shown.
60 changes: 60 additions & 0 deletions udp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#coding: utf-8
from udp_functions import *
from pymongo import MongoClient
import hashlib
import time

if __name__ == "__main__" :
HOST = ""
PORT = 6009
ADDR = (HOST, PORT)
BUFSIZE = 1024 * 10

udpSock = create_udp_socket(ADDR)

while (1):
print ("Waiting...")
data, addr = receive_message(udpSock, BUFSIZE)
send_message(udpSock, addr, "3team server")

MONGO_ADDR = "127.0.0.1:27017"
connection = MongoClient(MONGO_ADDR)
db = connection.music_db
collection = db.music_list

msg = ""
for item in collection.find():
list_str = item['rank'] + '. ' + item['music'] + '\n'
msg += list_str

send_message(udpSock, addr, msg) # client 에게 music list 전송
number, addr = receive_message(udpSock, BUFSIZE)

path = "D:/2017_S.W/1st/"
file_name = collection.find({"rank": number})[0]["music"]
file_name = path + file_name + ".mp3"

print(file_name.encode('utf-8') + " 파일 전송...")

hasher = hashlib.sha224()
with open(file_name, "rb") as f:
line = f.read()
line_length = len(line)
for i in range(0, line_length, BUFSIZE):
buf = line[i:i + BUFSIZE]
hasher.update(buf)
send_message(udpSock, addr, buf)
time.sleep(0.01)
#reply, addr = receive_message(udpSock, BUFSIZE)
#print ('sending buf...'),; print i,;
#print('clear buf'),;
send_message(udpSock, addr, "EOF")

time.sleep(0.5)
file_size = os.path.getsize(file_name)

send_message(udpSock, addr, str(file_size))
reply, addr = receive_message(udpSock, BUFSIZE)
send_message(udpSock, addr, str(hasher.hexdigest()))
reply, addr = receive_message(udpSock, BUFSIZE)
print(file_name.encode('utf-8') + " 파일 전송 완료")

0 comments on commit a29052e

Please sign in to comment.