python-netcat

本文介绍了如何使用Python模拟netcat进行监听和通信。首先展示了Linux虚拟机中nc命令的使用步骤,包括服务器端监听和客户端发送数据的过程。然后探讨了Python替代netcat的功能,通过逻辑图解释了在同一台Linux虚拟机和不同主机间如何实现客户端和服务器端的通信。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.Linux虚拟机中自带的nc与win主机实现监听并返回的简单实例:

监听端listing.py 监听

import socket
import threading

bind_ip   = "10.21.21.120"
bind_port = 9999

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("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()

这里写图片描述

(1)主机(服务器端listing.py)开始监听,返回提示正在主机IP上实行监听
(2)链接虚拟机nc
(3)主机接收到nc信息,提示接收的来自哪个IP Port
(4)nc输入字符串
(5)主机接收到输入的字符串,打印输出,并显示,返回一个应答给虚拟机
(6)nc接收到主机返回的应答,打印输出。

注意:这里是nc客户端与主机服务端监听同时使用,如果只有nc,这会提示无法连接。

二.python 取代netcat
同时实现客户端,服务器端

#!/usr/bin/python

import sys
import socket
import getopt
import threading
import subprocess


# define some global variables
listen             = False
command            = False
upload             = False
execute            = ""
target             = ""
upload_destination = ""
port               = 0

# this runs a command and returns the output
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值