yongyong-e
[Network] TCP file Transfer - 2 본문
local server에서 local client로 파일전송 구현 및 앞선 코드변경
# Server
# serverTest3.py
import socket
HOST = socket.gethostname()
PORT = 50000
ADDR = (HOST, PORT)
BUFF_SIZE = 1024
serverSocket = socket.socket()
serverSocket.bind(ADDR)
serverSocket.listen(5)
print ('Server waiting...')
while True:
clientSocket, addr = serverSocket.accept()
print ('Connection from', addr)
data = clientSocket.recv(BUFF_SIZE)
print('client : ' + data.decode())
filename='iu.jpg'
f = open(filename,'rb')
l = f.read(BUFF_SIZE)
while (l):
print('sending data...')
clientSocket.send(l)
print('(sent)',repr(l))
l = f.read(BUFF_SIZE)
f.close()
print('Done sending')
clientSocket.close()
# Client
# clientTest3.py
import socket
HOST = socket.gethostname()
PORT = 50000
ADDR = (HOST, PORT)
BUFF_SIZE = 1024
clientSocket = socket.socket()
clientSocket.connect(ADDR)
clientSocket.send('client message'.encode())
with open('serverFile.jpg', 'wb') as f:
print ('file opened')
while True:
print('receiving data...')
data = clientSocket.recv(BUFF_SIZE)
print('(data)' , data)
if not data:
break
f.write(data)
f.close()
print('Successfully get the file')
clientSocket.close()
print('connection closed')
'프로그래밍 > Python' 카테고리의 다른 글
[Algorithm] 문자열 뒤집기 (0) | 2017.09.01 |
---|---|
[Network] TCP multi-Thread (3) | 2017.08.12 |
[Network] TCP file Transfer - 1 (0) | 2017.08.10 |
[Network] TCP simple example (0) | 2017.08.09 |
[Ubuntu16.04] PyCharm Python IDE 설치 (0) | 2017.08.08 |