使用 ncat 在套接字上运行 python 脚本?

jug*_*108 1 command-line networking python

我想让一个 python 脚本在 LAN 中的服务器套接字上运行。我写了一个数字猜测脚本,我想让它在套接字上运行,以便其他客户端通过连接到端口(比如 1234)来使用它。我知道可以通过 python 的套接字编程来实现这一点。但这个问题是问为什么这会失败?

ncat 192.168.0.108 -lvp 1234 -e /usr/bin/python3.5 number_game.py

剧本:

#!/usr/bin/python3.5
import random
num=random.randint(1,20)
flag=0
print("Welcome to the game...")
for i in range(1,7):
    print("Take a guess")
    guess=int(input())
    if guess > num:
        print("Way too high")
    else:
        if guess < num:
            print("Way too low")
        else:
            flag=1
            break;
if flag == 1:
    print("You made it in "+str(i)+" attempts")
else:
    print("better luck next time")
Run Code Online (Sandbox Code Playgroud)

错误:

Ncat: Version 7.31 ( https://nmap.org/ncat ) Ncat: Got more than one port specification: 1234 number_game.py. QUITTING.

Sid*_*med 5

解决方案

您正在尝试侦听端口1234,并同时连接到具有 IP 的机器192.168.1.108

你不能这样做,你要么使用这个监听连接:

ncat -lvp 1234 -e "/usr/bin/python3.5 number_game.py"`
Run Code Online (Sandbox Code Playgroud)

或者您使用以下命令启动与所需机器的连接:

ncat -v -e "/usr/bin/python3.5 number_game.py" 192.168.0.108 1234
Run Code Online (Sandbox Code Playgroud)

笔记

当您使用ncat(or nc) 启动连接时,您必须保留IP(or hostname) 和port最后一个参数。

查看ncat手册中的概要:ncat [OPTIONS...] [hostname] [port]