サイバーセキュリティプログラミングという本でkali-linuxを用いてクライアントとサーバーの通信をするプログラムをそのまま書き写して実行したのですが、よく分からない点があるのです。target_hostがgoogleとなっているのですが実行して大丈夫なのでしょうか?このプログラムでどうやって通信をすればいいのでしょうか?初心者なので教えてほしいです!
TCPクライアント
coding: utf-8
import socket
target_host = "www.google.com"
target_port = 80
socketobject create
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect to server
client.connect((target_host,target_port))
#transmission of data
client.send("GET /HTTP/1.1\r\nHost: google.com\r\n\r\n")
#reception of data
response = client.recv(4096)
print response
TCPサーバー
-- coding: utf-8 --
import socket
import threading
bind_ip = "127.0.0.1"
bind_port = 9999
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print "[*] Listening on {0}:{1}".format(bind_ip,bind_port)
The thread which handles the connection from a client
def handle_client(client_socket):
I handle the data which a client transmitted
request = client_socket.recv(1024)
print("[*] Received: ", request)
return of packet
client_socket.send(b"ACK!")
client_socket.close()
while True:
client,addr = server.accept()
print("[*] Accepted connection from: {0}:{1}".format(addr[0],addr[1]))
Start of a thread processing reception data
client_handler = threading.Thread(target=handle_client,args=(client,))
clinent_handler.start()
回答1件
あなたの回答
tips
プレビュー