Skip to content

Commit

Permalink
소켓을 이용한 간단한 서버
Browse files Browse the repository at this point in the history
  • Loading branch information
sohwaje authored May 17, 2020
1 parent c0b27cc commit 817f0d8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions socket_blocking_nonblocking_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import socket

def test_socket_modes():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 소켓을 생성한다.
s.setblocking(0) # 소켓을 블록킹 모드로 설정한다.
s.settimeout(0.5) # 소켓의 타임 아웃을 0.5로 설정한다.
s.bind(("127.0.0.1", 0))
# 소켓을 address에 바인딩 한다. 바인딩 포트를 자동으로 지정하려면 0을, 수동으로 하려면 0대신 적절한 포트를 할당한다.

socket_address = s.getsockname() # 소켓 자신의 주소를 반환한다.
print("Trivial Server lauched on socket: %s" % str(socket_address))
while(1):
s.listen(2) # listen(2)에 지정된 개수만큼 서버가 연결을 수락한다.

if __name__ == '__main__':
test_socket_modes()

0 comments on commit 817f0d8

Please sign in to comment.