I am new to Socket Programming in Python. I have written the following code in Python 3.7:
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:
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. I have written the above few lines of code after reading this API.
* I can run tnc
command if I hit it directly in Windows cmd.
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
. 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)
Here is the listed issue.
tnc
is a PowerShell command . You'll need to explicitly run it with PowerShell like this:
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. Either the program is not installed at all, or---if it is installed---it isn't in the PATH variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.