【问题标题】:How execute python3 script remotely via cURL?如何通过 cURL 远程执行 python3 脚本?
【发布时间】:2019-10-08 01:30:56
【问题描述】:

我这里有这个文件

# --------------------------------------------------------------
# Goal : Remove file base on input match
# Run  : curl 45.55.88.57/code/fileModifier.py  | python3


import os
import sys

rootdir = os.path.abspath(os.curdir)
print(rootdir)




#Give string to remove from folder names. Ensure that removing a string doens't make the folder name empty. It wont work
removeStringFromFolderName = input('Remove this from folder names :')
while removeStringFromFolderName == '':
    print('Empty string not allowed')
    removeStringFromFolderName = input('Remove this file if contain : ')

count = 0
subdir = [x for x in os.walk(rootdir)]
toRemove = []
for chunk in subdir:
    folders = chunk[1]
    if len(folders) > 0:
        for aDir in folders:
            if removeStringFromFolderName in aDir:
                toRemove.append((chunk[0], aDir))


toRemove.reverse()

for folders in toRemove:
    oldPath = (os.path.join(folders[0], folders[1]))
    newPath = (os.path.join(folders[0], folders[1].replace(removeStringFromFolderName,'')))
    os.rename(oldPath, newPath)
    count +=1

subdir = [x for x in os.walk(rootdir)]
for chunk in subdir:
    folders = chunk[1]
    if len(folders) > 0:
        for aDir in folders:
            if removeStringFromFolderName in aDir:
                print(os.path.join(chunk[0], aDir))
                oldPath = (os.path.join(chunk[0], aDir))
                newPath = (os.path.join(chunk[0], aDir.replace(removeStringFromFolderName,'')))
                os.rename(oldPath, newPath)
                count +=1

print('Renamed', count, 'files')

count = 0
#Give string to delete files which contain this string
removeThisFileNameIfContain = input('Enter string to delete files which contain this string: ')
while removeThisFileNameIfContain == '':
    print('Empty string not allowed')
    removeThisFileNameIfContain = input('Enter string to delete files which contain this string: ')

for subdir, dirs, files in os.walk(rootdir):
    for aFile in files:
        if '.py' in aFile:
            continue
        if removeThisFileNameIfContain in aFile:
            os.remove(os.path.join(subdir, aFile))
            count += 1
print('Deleted', count, 'files')

在本地机器上使用 python3 时工作完美,但当我上传到我的虚拟机并通过 cURL 远程执行时

我一直收到这个

curl 45.55.88.57/code/fileModifier.py  | python3
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2266  100  2266    0     0  43381      0 --:--:-- --:--:-- --:--:-- 43576
/Users/bheng/Desktop/projects/bheng/fileModifier
Remove this from folder names :Traceback (most recent call last):
  File "<stdin>", line 16, in <module>
EOFError: EOF when reading a line

我错过了什么?

【问题讨论】:

    标签: python python-3.x curl wget


    【解决方案1】:

    您的使用占用了input 命令所需的标准输出。

    如果你的 shell 有能力试试这个:

    python3 <(curl 45.55.88.57/code/fileModifier.py)
    

    注意:正如 Amadan 所说,您的语法(和我的)在本地运行远程脚本,反之亦然。

    【讨论】:

      【解决方案2】:

      首先,您不是在远程执行脚本。您正在获取它,然后通过管道进入 Python REPL 来执行它本地

      当您将脚本导入 Python 时,stdin 是程序的来源。您不能使用stdin 也从input() 获取数据。

      要真正远程执行它,您需要在代码中构建 Web 服务器,或者告诉现有的 Web 服务器运行您的 Python 代码(例如,通过注册 CGI 处理程序)。

      【讨论】:

        猜你喜欢
        • 2018-09-15
        • 2020-09-21
        • 1970-01-01
        • 2021-02-17
        • 2011-05-09
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        相关资源
        最近更新 更多