http://www.hackerschool.org/HS_Boards/zboard.php?id=Free_Lectures&no=8579 [º¹»ç]
Ŭ¶óÀ̾ðÆ® ±¸Çö
¼¹ö¿¡ µ¥ÀÌÅ͸¦ ÁÖ°í ¹Þ±â À§ÇØ ¼ÒÄÏ ¿¬°á ¿äûÀ» ÇÏ´Â ÃøÀ¸·Î
IP¿Í Æ÷Æ®¹øÈ£·Î ¼¹ö¿¡ ¿¬°áÀ» ¿äûÇÑ´Ù.
ÄÚµå ¿¹Á¦¸¦ º¸±â ÀÌÀü¿¡ setsockopt ¸Þ¼Òµå¿¡ ´ëÇØ ¾Ë¾Æº¸°Ú´Ù.
---------------------------------------------------------------
socket.setsockopt(level, optname, None, optlen: int)
Set the value of the given socket option (see the Unix manual page setsockopt(2)).
The needed symbolic constants are defined in this module
(SO_* etc. <socket-unix-constants>).
The value can be an integer,
None or a bytes-like object representing a buffer.
In the later case it is up to the caller to ensure
that the bytestring contains the proper bits
(see the optional built-in module struct for a way to encode C structures as bytestrings).
When value is set to None, optlen argument is required.
It¡¯s equivalent to call setsockopt() C function with optval=NULL and optlen=optlen.
Çؼ®
ÁÖ¾îÁø ¼ÒÄÏ ¿É¼ÇÀÇ °ªÀ» ¼³Á¤ÇÕ´Ï´Ù. ÇÊ¿äÇÑ »ó¼ö´Â ÀÌ ¸ðµâ¿¡¼ Á¤Àǵ˴ϴÙ
SO_* µî, <socket-unix-constants>¸¦ ÂüÁ¶Çϼ¼¿ä).
°ªÀº Á¤¼ö, None ¶Ç´Â ¹öÆÛ¸¦ ³ªÅ¸³»´Â ¹ÙÀÌÆ®·ù °´Ã¼ÀÏ ¼ö ÀÖ½À´Ï´Ù.
ÈÄÀÚÀÇ °æ¿ì È£ÃâÀÚ°¡ ¿Ã¹Ù¸¥ ºñÆ®¸¦ Æ÷ÇÔÇÏ´ÂÁö È®ÀÎÇØ¾ß ÇÕ´Ï´Ù
(¿É¼ÇÀ¸·Î ³»Àå ¸ðµâ struct¸¦ »ç¿ëÇÏ¿© C ±¸Á¶Ã¼¸¦ ¹ÙÀÌÆ®¿·Î ÀÎÄÚµùÇÏ´Â ¹æ¹ýÀ» ÂüÁ¶Çϼ¼¿ä).
°ªÀÌ NoneÀ¸·Î ¼³Á¤µÈ °æ¿ì optlen Àμö°¡ ÇÊ¿äÇÕ´Ï´Ù.
ÀÌ°ÍÀº optval=NULL ¹× optlen=optlenÀ¸·Î setsockopt() C ÇÔ¼ö¸¦ È£ÃâÇÏ´Â °Í°ú µ¿µîÇÕ´Ï´Ù.
-----------------------------------------------------------------
ÀÚ¼¼ÇÑ °Ç ¾Æ·¡ÀÇ ¸µÅ©·Î...
https://docs.python.org/ko/3.10/library/socket.html
Ŭ¶óÀ̾ðÆ® Ãø ¼Ò½º
import socket
server_host = 127.0.0.1 # Loopback IP
server_port = 44444
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#connect to server
client_sock.connect((server_host, server_port))
print(f'Server: {server_host}, {server_port} connect nomally')
while True:
message = input(">>> ")
client_sock.sendall(message.encode('utf-8'))
message = client_sock.recv(1024)
print(f"server: {message.decode()}")
client_sock.close()
¼¹ö ±¸Çö & ¼¹ö Ãø ¼Ò½º
import socket
host = '127.0.0.1'
port = 44444
parent_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
parent_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
parent_sock.bind((host, port))
parent_sock.listen(5)
print(f'Server IP: {host}, Port Num: {port} excuting to server...')
child_sock, child_addr = parent_sock.accept()
print(f'accept from {child_addr}')
while True:
message = child_sock.recv(1024)
print(f'{child_addr}: {message.decode()}')
child_sock.sendall(message)
child_sock.close()
parent_sock.close()
|
Hit : 832 Date : 2024/05/28 11:45
|