πŸ‘©πŸΌβ€πŸ’» python socket

may_soouuΒ·2020λ…„ 12μ›” 23일
0
post-thumbnail

1. μ†ŒμΌ“μ΄λž€?

: 두 λŒ€μ˜ 컴퓨터 μ‚¬μ΄μ—μ„œ TCP/IP λ„€νŠΈμ›Œν¬λ₯Ό 톡해 μƒν˜Έ 톡신이 κ°€λŠ₯ν•˜λ„λ‘ μ²˜λ¦¬ν•΄μ£ΌλŠ” λ°©μ‹μž…λ‹ˆλ‹€. 즉 컴퓨터 간에 데이터λ₯Ό μ£Όκ³  λ°›κΈ° μœ„ν•œ μ–‘λ°©ν–₯ ν†΅μ‹ μž…λ‹ˆλ‹€.

2. νŒŒμ΄μ¬μ—μ„œ μ†ŒμΌ“μ„ μ‚¬μš©ν•˜λ €λ©΄?

μ†ŒμΌ“μ„ μ΄μš©ν•΄ μ„œλ²„-ν΄λΌμ΄μ–ΈνŠΈ μ†ŒμΌ“μ„ κ΅¬μ„±ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

2-1. μ„œλ²„ μ†ŒμΌ“(server.py)

1. λͺ¨λ“ˆ μž„ν¬νŠΈ

import socket

2. μ„œλ²„ μ†ŒμΌ“ 생성

socket("도메인", "μ†ŒμΌ“μ˜ μœ ν˜•")
# socket ν•¨μˆ˜μ— λ‘κ°œμ˜ 인자λ₯Ό λ°›μŠ΅λ‹ˆλ‹€.

> serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  • λ„λ©”μΈμ˜ μ’…λ₯˜λŠ” AF~~ / PF~~ 둜 μ‹œμž‘ν•˜λŠ” ν”„λ‘œν† μ½œμ΄ μžˆμŠ΅λ‹ˆλ‹€.
    - AF_INET은 μ£Όμ†Œ ν˜•μ‹μ΄λ©°, κ°€μž₯ 일반적으둜 μ‚¬μš©λ˜λŠ” IPv4 μ†ŒμΌ“μž…λ‹ˆλ‹€.
    - μ£Όμ†Œ ν‘œν˜„μ„ μœ„ν•΄ (host, port) νŠœν”Œμ΄ μ‚¬μš©λ©λ‹ˆλ‹€.

  • μ†ŒμΌ“μ˜ μœ ν˜•μ€
    SOCK_STREAM : TCP μ†ŒμΌ“
    SOCK_DGRAM : UDP μ†ŒμΌ“
    SOCK_RAW : 무가곡 μ†ŒμΌ“

3. μ†ŒμΌ“ 맀핑

socket.bind(HOST, PORT)

> socket.bind(('127.0.0.1', 10000))
  • 2λ²ˆμ—μ„œ λ§ν•œ μ£Όμ†Œ ν‘œν˜„μ„ μœ„ν•΄ μ‚¬μš©λ˜λŠ” νŠœν”Œ ν˜•νƒœμž…λ‹ˆλ‹€.
  • μ†ŒμΌ“ 객체에 κ³ μœ ν•œ ν˜ΈμŠ€νŠΈμ™€ 포트λ₯Ό μ—°κ²°ν•¨μœΌλ‘œμ¨, ν”„λ‘œκ·Έλž¨ μΈν„°νŽ˜μ΄μŠ€μ™€ λ„€νŠΈμ›Œν¬ 지원을 μ—°κ²°ν•©λ‹ˆλ‹€.
  • 포트번호의 μ’…λ₯˜

    1) 0 ~ 1023번 : 잘 μ•Œλ €μ§„ 포트 (Well-Known Port)
    ex. HTTP(80), HTTPS(443), DNS(53)

    2) 1024 ~ 49151번 : λ“±λ‘λœ 포트 (Registered Port)

    3) 49152 ~ 65535번 : 동적 포트 (Dynamic Port)

    μ—¬κΈ°μ„œ μš°λ¦¬κ°€ μž„μ˜λ‘œ μ„œλ²„λ₯Ό κ΅¬μΆ•ν•΄μ„œ 톡신을 ν•΄λ³΄κ³ μž ν•  λ•Œ, μ‚¬μš©ν•  수 μžˆλŠ” ν¬νŠΈλ²ˆν˜ΈλŠ” 2)번(μž„μ‹œ 포트번호) μž…λ‹ˆλ‹€.

4. μ—°κ²° μš”μ²­ λŒ€κΈ° μƒνƒœ μ„€μ •

serverSocket.listen(ν•œλ²ˆμ— μ²˜λ¦¬ν•  수 μžˆλŠ” μ—°κ²°μ˜ 수)

serverSocket.listen(5)
  • μ„œλ²„μ—λ§Œ ν•„μš”ν•œ μ„€μ •μž…λ‹ˆλ‹€.
  • μ—°κ²° μš”μ²­ λŒ€κΈ°λ₯Ό ν•œ 이후에 연결이 κ°€λŠ₯ν•˜λ©°, μ‹€μ œ 연결이 되면 μƒˆλ‘œμš΄ μ†ŒμΌ“μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

5. μ—°κ²° 수락

clientSocket, addr = serverSocket.accept()
print('addr')
 # μ ‘μ†ν•œ ν΄λΌμ΄μ–ΈνŠΈμ˜ μ£Όμ†Œκ°€ ν”„λ¦°νŠΈ λ©λ‹ˆλ‹€.
print(clientSocekt)
 # μ ‘μ†ν•œ ν΄λΌμ΄μ–ΈνŠΈμ˜ μ†ŒμΌ“μ΄ ν”„λ¦°νŠΈ λ©λ‹ˆλ‹€.
print('--client information--')

# accept μ•ž 이름(serverSocket)은 2λ²ˆμ—μ„œ μ„œλ²„ μ†ŒμΌ“μ‹œ μƒμ„±ν•œ λ³€μˆ˜λͺ…μž…λ‹ˆλ‹€.
  • μ—°κ²° μš”μ²­ λŒ€κΈ° 쀑인 μ†ŒμΌ“μ΄ accept() ν•¨μˆ˜μ—μ„œ λŒ€κΈ°ν•˜λ‹€κ°€, ν΄λΌμ΄μ–ΈνŠΈκ°€ μ ‘μ†ν•˜λ©΄ μƒˆλ‘œμš΄ μ†ŒμΌ“μ„ λ¦¬ν„΄ν•©λ‹ˆλ‹€.

6. ν΄λΌμ΄μ–ΈνŠΈλ‘œλΆ€ν„° 메세지 κ°€μ Έμ˜€κΈ°

while True:
    data = clientSocket.recv(1024)
    # data = 5λ²ˆμ—μ„œ μ„€μ •ν•œ λ³€μˆ˜λͺ….recv(byte크기)
	if data:
           clientSocket.sendall(data)
            # μˆ˜μ‹ λœ 데이터λ₯Ό ν΄λΌμ΄μ–ΈνŠΈμ—κ²Œ λ‹€μ‹œ μ „λ‹¬ν•©λ‹ˆλ‹€.
        else:
           break
            # 데이터가 λΉ„μ–΄μžˆμœΌλ©΄ 루프λ₯Ό 멈μΆ₯λ‹ˆλ‹€

# μ†ŒμΌ“ μ’…λ£Œ
clientSocekt.close()
serverSocket.close()

7. μ„œλ²„ μ†ŒμΌ“.py 전체 μ½”λ“œ

import socket

serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(('127.0.0.1', 10000))
serverSocket.listen(5)
clientSocket, addr = serverSocket.accept()

while True:
    data = clientSocket.recv(1024)
	if data:
           clientSocket.sendall(data)
        else:
           break

clientSocket.close()
serverSocket.close()

2-1. ν΄λΌμ΄μ–ΈνŠΈ μ†ŒμΌ“(client.py)

1. λͺ¨λ“ˆ μž„ν¬νŠΈ

import socket

2. μ„œλ²„ μ†ŒμΌ“ 생성

socket("도메인", "μ†ŒμΌ“μ˜ μœ ν˜•")
# socket ν•¨μˆ˜μ— λ‘κ°œμ˜ 인자λ₯Ό λ°›μŠ΅λ‹ˆλ‹€.

> clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

3. μ„œλ²„ 접속 μ‹œλ„

clientSocket.connect(("127.0.0.1", 10000))
# coonect μ•ž λ³€μˆ˜λͺ…은 2μ—μ„œ μ§€μ •ν•œ λ³€μˆ˜λͺ…μž…λ‹ˆλ‹€.
# μ„œλ²„μ†ŒμΌ“ - 3λ²ˆμ—μ„œ μ„€μ •ν•œ ν˜ΈμŠ€νŠΈμ™€ 포트 번호λ₯Ό λ„£μ–΄μ€λ‹ˆλ‹€.

4. 메세지 μ „μ†‘ν•˜κΈ°

try:
    clientSocket.send('Hello! I"m client'.encode())
    msg = clientSocket.recv(1024)
    print(msg)
    
finally:
    print('closing socket')
    clientSocket.close()
  • connect ν•¨μˆ˜μ™€ μ—°κ²°λœ 이후뢀터 μ„œλ²„μ™€ λŒ€ν™”κ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€.

5. ν΄λΌμ΄μ–ΈνŠΈ μ†ŒμΌ“.py 전체 μ½”λ“œ

import socket

clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(("127.0.0.1", 10000))

try:
    clientSocket.send('Hello! I"m client'.encode())
    msg = clientSocket.recv(1024)
    print(msg.decode())

finally:
    print('closing socket')
    clientSocket.close()

3. 싀행해보기

serversocket.py 와 clientsocket.py 두 개의 νŒŒμΌμ„ μƒμ„±ν•˜μ—¬
μœ„μ˜ μ½”λ“œλ₯Ό λΆ™μ—¬ λ„£μŠ΅λ‹ˆλ‹€.

터미널 λ‘κ°œλ₯Ό μ—΄κ³ ,
serversocket.py λ¨Όμ € μ‹€ν–‰ν•œλ‹€μŒμ—

$ python serversocket.py

clientsocket.py λ₯Ό μ‹€ν–‰ν•˜λ©΄ λ©λ‹ˆλ‹€.
μ½”λ“œ 쀑간쀑간 printλ₯Ό μ°μ–΄μ„œ 값을 확인 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

$ python clientsocket.py

μ°Έκ³  1
μ°Έκ³  2

profile
back-end 개발자

0개의 λŒ“κΈ€