-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("완료") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') + " 파일 전송 완료") |