[英]Check if a socket is busy or not
I am new to Socket Programming in Python. 我是Python中的Socket Programming的新手。 I have written the following code in Python 3.7:
我在Python 3.7中编写了以下代码:
trialSocketList.py trialSocketList.py
import subprocess
import sys
HOST = sys.argv[1]
PORT = sys.argv[2]
command = "tnc " + HOST + " -PORT "
print(command)
subprocess.call(command + PORT)
I am passing the following in the Windows CMD: 我在Windows CMD中传递以下内容:
python trialSocketList.py "127.0.0.1" 445
But I am having the following error while executing the above code: 但是在执行上面的代码时出现以下错误:
tnc 127.0.0.1 -PORT
Traceback (most recent call last):
File "trialSocketList.py", line 14, in <module>
subprocess.call(command + PORT)
File "C:\Python37\lib\subprocess.py", line 323, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Python37\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
When I try netstat -an
instead of the command tnc 127.0.0.1 -PORT
in the same code, the code functions perfectly. 当我在相同的代码中尝试
netstat -an
而不是命令tnc 127.0.0.1 -PORT
时,代码完美地运行。 I have written the above few lines of code after reading this API. 我在阅读此 API后编写了以上几行代码。
* I can run tnc
command if I hit it directly in Windows cmd. * 如果我直接在Windows cmd中点击它,我可以运行
tnc
命令。
Am I missing something here? 我在这里错过了什么吗? Or is there any other better way of doing this?
或者还有其他更好的方法吗? If so, then please help me understand the issue here.
如果是的话,请帮助我理解这里的问题。
Thanks in advance. 提前致谢。
Try calling Popen
with shell=True
. 尝试使用
shell=True
调用Popen
。 Here is how your code would look with that: 以下是您的代码的外观:
import subprocess
import sys
HOST = sys.argv[1]
PORT = sys.argv[2]
command = "tnc " + HOST + " -PORT "
print(command)
process = subprocess.Popen(command, stdout=tempFile, shell=True)
tnc
is a PowerShell command . tnc
是一个PowerShell命令 。 You'll need to explicitly run it with PowerShell like this: 您需要使用PowerShell显式运行它,如下所示:
import subprocess
import sys
HOST = "127.0.0.1"
PORT = 445
command = "tnc " + HOST + " -PORT " + str(PORT)
print(command)
subprocess.call(["powershell.exe",command],stdout=sys.stdout)
Output: 输出:
tnc 127.0.0.1 -PORT 445
ComputerName : 127.0.0.1
RemoteAddress : 127.0.0.1
RemotePort : 445
InterfaceAlias : Loopback Pseudo-Interface 1
SourceAddress : 127.0.0.1
TcpTestSucceeded : True
The issue here is that the python script can't find the tnc
program. 这里的问题是python脚本找不到
tnc
程序。 Either the program is not installed at all, or---if it is installed---it isn't in the PATH variable. 程序根本没有安装,或者---如果已安装---它不在PATH变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.