Sharpen Python skills in the field of cybersecurity.
讓我們在資安領域磨利Python技術。
Let's get started! 開始吧!
⭐ 可以查看之前的教學
在Python建伺服器(Server),就和建立客戶端一樣簡單(參考這一篇)
###🔸建立步驟,如下列所示:
# tcp_server.py
import socket
import threading
bind_ip = "0.0.0.0"
bind_port = 9998
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print("[*] Listening on %s:%d" % (bind_ip, bind_port))
# this is our client handling thread
def handle_client(client_socket):
# just print out what the client sends
request = client_socket.recv(1024)
print("[*] Received: %s" % request)
# send back a packet
client_socket.send(b"ACK!")
print(client_socket.getpeername())
client_socket.close()
while True:
client, addr = server.accept()
print("[*] Accepted connection from: %s:%d" % (addr[0], addr[1]))
# spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()
TCP Client
# tcp_client.py
import socket
target_host = "127.0.0.1"
target_port = 9998
# create a socket object
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
client.connect((target_host, target_port))
# send some data
client.send(b"GET / HTTP/1.1 \r\nHost: hacker.com\r\n\r\n")
# receive data
response = client.recv(4096)
print(response.decode())
client.close()
先跑伺服器
再跑客戶端
Reference參考資料
推薦影片
絕讚! Youtube 教學影片 | Elevate Cyber