python - EOFError : EOF when reading a line only when execute it via curl 错误

标签 python curl

我有一个基于 URL 和用户输入数据构建 CURL 命令的 python 代码


我有

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

我得到了

______________

   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


执行要点

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

结果

______________

   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 上运行的结果不同?

我该如何预防?

最佳答案

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

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

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

关于python - EOFError : EOF when reading a line only when execute it via curl 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293959/

相关文章:

php - 无法从HTML页面获取表格数据

python - 使用 python boto3 使用 s3 和 cloudfront 部署静态站点

python - 如何按索引级别和值对分组的多索引 pandas 系列进行排序?

python - 查找包含子字符串的列并替换它 - Pandas

php - PayPal TLS 测试 URL - PHP curl SSL 协议(protocol)错误

python - 使用curl获取csv的shell脚本

php - 通过我的php代码以其他网站的形式填写数据

javascript - 如何将curl转换为javascript post请求

访问 cv2.VideoCapture(0) 的 Python 意外退出

python - 使用 Python 请求的重定向错误太多