본문 바로가기
Language/Python

TCP 클라이언트

by chmod640 2022. 8. 17.

 

import socket

target_host = "www.google.com"
target_port = 80
target_msg = "GET / HTTP/1.1\r\nHOST: koogle.com\r\n\r\n"


client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_port))

client.send(target_msg.encode('utf-8'))

response = client.recv(4096).decode('utf-8')

print(response)

8 : AF_INET(IPv4 또는 호스트명 사용), SOCK_STREAM(TCP 사용) 매개변수 이용해 소켓 객체 생성

9: 서버 접속

11: HTTP Message Request

13: Response Message 저장

댓글