首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EOFError:仅在通过curl执行行时读取行时的EOF

EOFError:仅在通过curl执行行时读取行时的EOF
EN

Stack Overflow用户
提问于 2016-10-28 04:48:24
回答 2查看 623关注 0票数 2

我有一段python代码,它根据URL和来自用户输入的数据构造CURL命令

我有过

代码语言:javascript
复制
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()

我在运行它

python get_curl.py

我得到了

代码语言:javascript
复制
______________

   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" }

然后,我把它托管在github gist上,这样我就可以分享它了

链接=

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

执行要点

代码语言:javascript
复制
curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N | python

结果

代码语言:javascript
复制
______________

   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

为什么我在本地Mac上运行时会有不同的结果?

我该如何预防呢?

EN

回答 2

Stack Overflow用户

发布于 2016-10-28 05:10:56

由于您通过管道将curl命令传递给python,因此将标准输入连接到管道,而不是用户的终端。

您可以使用bash process substitution使curl命令显示为文件名参数,而不是从标准输入中读取它。

代码语言:javascript
复制
python <(curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N)
票数 3
EN

Stack Overflow用户

发布于 2016-10-28 05:06:48

在python等待输入时,管道正在发送行。最好做一些像这样的事情

代码语言:javascript
复制
curl "https://gist.githubusercontent.com/bheng/b23d775ee7b106cd7cc0ae5ac71b81a9/raw/c6ecd3ed7bc04699d73e1b9ed521f481ac6a41c4/get_curl.py" -s -N > myscript; python myscript
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40293959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档