code-8
code-8

Reputation: 58730

How execute python3 script remotely via cURL?

I have this file here

# --------------------------------------------------------------
# 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')

Work perfect when on local machine with python3, but when I uploaded into my VM, and executed remotely via cURL

I kept getting this

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

What did I missed ?

Upvotes: 0

Views: 107

Answers (2)

Pi Marillion
Pi Marillion

Reputation: 4674

Your usage is taking up stdout, which the input command needs.

Try this if your shell has the ability:

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

Note: As Amadan said, your syntax (and mine) run a remote script locally, not vice versa.

Upvotes: 1

Amadan
Amadan

Reputation: 198436

First of all, you are not executing the script remotely. You are fetching it then executing it locally by piping into Python REPL.

When you are piping the script into Python, stdin is where the program comes from. You cannot use stdin to also get data from input().

To actually execute it remotely, you would need to either build in a web server in your code, or tell an existing web server to run your Python code (e.g. by registering a CGI handler).

Upvotes: 0

Related Questions