2016-10-27 2 views
2

I einen Python-Code haben, der einer Aufrollung Befehls Basis auf URL und Daten von BenutzereingabenEOFError: EOF, wenn eine Zeile nur lesen, wenn es curl über ausführen


Konstrukt I haben

import os 

print ("______________\n") 
print " 1.GET   " 
print " 2.POST  " 
print " 3.PUT   " 
print " 4.DELETE  " 
print ("______________\n") 


http = int(raw_input("Select your option : ")) 

url = raw_input("Paste Your URL : ") 

if not http: 
    http = 3 

if http == 1: 
    cmd = 'curl '+ url 
elif http == 2: 
    data = raw_input("Paste Your Data : ") 
    cmd = 'curl -g -X POST -H "Content-Type:application/json" -d \''+data+'\' ' + url 
elif http == 3: 
    data = raw_input("Paste Your JSON Data: ") 
    cmd = 'curl -g -X PUT -H "Content-Type:application/json" -d \''+data+'\' ' + url 
else: 
    cmd = 'curl -g -X DELETE ' + url 

print ("_________________________________________\n") 
print '\n' 
print cmd 
print '\n' 
print ("_________________________________________\n") 


run = raw_input("Do you want to run it ? [y/n]: ") 
print '\n' 
if run == 'y': 
    os.system(cmd+'\n') 
    print '\n' 
else: 
    os.system("clear") 
    sys.exit() 

ich betreibe es

python get_curl.py

Ich habe

______________ 

    1.GET   
    2.POST   
    3.PUT   
    4.DELETE  
______________ 

Select your option : 3 
Paste Your URL : http://172.19.242.32:1234/vse/vcpe/002233445567/vlan/104/device/000011223350/duration 
Paste Your Data : {"acl_mode": 2, "portal_url": "http://localhost:8888/captive-portal?client_mac=$MAC&ap=$AP-MAC", "duration": 120 } 
_________________________________________ 



curl -g -X PUT -H "Content-Type:application/json" -d '{"acl_mode": 2, "portal_url": "http://localhost:8888/captive-portal?client_mac=$MAC&ap=$AP-MAC", "duration": 120 }' http://172.19.242.32:1234/vse/vcpe/002233445567/vlan/104/device/000011223350/duration 


_________________________________________ 

Do you want to run it ? [y/n]: y 
{ "status": 201, "message": "Processed cpe HNS device duration message" } 

Dann habe ich es auf Github Kern Host, also kann ich es den Kern

link =

https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py


Execute teilen

curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N | python 

Ergebnis

______________ 

    1.GET   
    2.POST   
    3.PUT   
    4.DELETE  
______________ 

Select your option : Traceback (most recent call last): 
    File "<stdin>", line 11, in <module> 
EOFError: EOF when reading a line 

Warum muss ich anderes Ergebnis haben, wie ich es auf meinem lokalen Mac ausgeführt haben würde?

Wie verhindere ich das?

+0

Vielleicht Python wird mit der Pipe verwirrt (unter Berücksichtigung, dass die 'stdin' und verursacht Probleme mit der' raw_input')? (So ​​verwendet es den Code des Skripts als Eingabe für das Skript?) – BorrajaX

Antwort

2

Die Pipe sendet Zeilen, während Python auf Eingabe wartet. Besser so etwas wie

curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N > myscript; python myscript 
+0

Guten Tipp Jungs :) – ihue

1

zu tun Da Sie den curl Befehl python kochend sind, stdin mit dem Rohr verbunden ist, nicht das Endgerät des Benutzers.

Sie können bash process substitution verwenden, um den Befehl curl als Dateinamenargument erscheinen zu lassen, anstatt ihn von stdin zu lesen.

python <(curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N) 
+0

Super Vorschlag !! – ihue

+0

Funktioniert perfekt, darf ich fragen, was ist das '<(...)'? Kann der von Ihnen vorgeschlagene Befehl auch auf einem beliebigen Betriebssystem ausgeführt werden? Windows, Linux und Mac? – ihue

+1

Es ist Prozesssubstitution. Ich habe einen Link zur Dokumentationsseite hinzugefügt. Wie es heißt: * Die Prozesssubstitution wird auf Systemen unterstützt, die named pipes (FIFOs) oder die/dev/fd-Methode zur Benennung von offenen Dateien unterstützen. * Es funktioniert auf Linux und Macs, ich weiß nicht, ob es unter Windows funktioniert. – Barmar

Verwandte Themen