본문 바로가기
프로그래밍/PYTHON

2. 파이썬 UDP 소켓 통신(PYTHON UDP SOCKET PROGRAM)

by 루티즈 2019. 11. 14.
반응형

image 파일 전송

client     -> server ( 이미지 파일 전송 )

< 1.PNG -> 2.PNG >

 

1.     Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import socket               # Import socket module
 
= socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
= open('2.png','wb')
s.listen(5)                 # Now wait for client connection.
while True:
    c, addr = s.accept()     # Establish connection with client.
    print ('Got connection from', addr)
    print ('Receiving...')
    l = c.recv(8096)
    while (l):
        print ('Receiving...')
        f.write(l)
        l = c.recv(8096)
    f.close()
    print ('Done Receiving')
    c.send('Thank you for connecting')
    c.close()                # Close the connec
 
 

 

2.     Client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import socket               # Import socket module
 
= socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                 # Reserve a port for your service.
 
s.connect((host, port))
= open('1.png','rb')
print ('Sending...')
= f.read(8096)
while (l):
    print ('Sending...')
    s.send(l)
    l = f.read(8096)
f.close()
print ('Done Sending')
s.shutdown(socket.SHUT_WR)
print (s.recv(8096))
s.close                     # Close the socket when done

 

3.     결과 확인.

 

반응형