This question already has answers here:
Why does cURL return error "(23) Failed writing body"?
(19 answers)
Closed 6 years ago.
I'm not famililar with the shell but i have to run this command from a Python file:
curl -XDELETE 'localhost:9200/event/_query?pretty' -d '{"query" : { "range" : { "int_timestamp" : { "lte" : "2016-06-09 08:55:10" } } } }'
From the file i use os.popen to execute the command. I have tried it also with os.system. In both cases it gives me the following Error:
(23) Failed writing body
But when i run this command in the shell, it worked perfect.
What did i wrong?
Thanks for help!
EDIT:
I don't know why because i doesn't change something. But now it worked with os.system instead of os.popen.
Anyway thanks for help!
The error (23) Failed writing body indicates you didn't properly wait for curl to output its data before closing the file descriptor. This can be caused by not calling read() after os.popen()
os.system can do the trick, but it doesn't seem like you can retrieve its standard output.
Related
This question already has an answer here:
Passing a url as argument
(1 answer)
Closed 2 years ago.
I have a Python script which uses sys to accept arguments -
import sys
url = sys.argv[1]
I need to provide it with a bunch of urls to parse. The script works perfectly on a jupytor notebook (after hard-coding the arguments in the code: url = 'http://www.hello.com') notebook but when I try to execute it as a script I get errors like these, for various URLs -
for 'http://www.blog.example.com:123/path/to/file.html?key1=value1'
[1] 85926
zsh: no matches found: http://www.blog.example.com:123/path/to/file.html?key1=value1
[1] + exit 1 python -m urlparser
for 'https://www.hello.com/photo.php?id=2064343443411&set=a.2634433167446&type=3&hall'
zsh: parse error near `&'
Meanwhile, the script works fine for simpler URLs like https://blog.hello.com/one/two
What could be the issue? Encoding problems?
I figured -
Have to put the arguments in quotes like - 'http://www.blog.example.com:123/path/to/file.html?key1=value1'
This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Closed 7 years ago.
I currently have the following code:
subprocess.call(["png2pos", "-c", "example_2.png", ">", "/dev/usb/lp0"])
The program png2pos is being accessed because it's giving me the message:
This utility produces binary sequence printer commands. Output have to
be redirected
This is the same error I get if I forget to type in > /dev/usb/lp0, so I'm fairly certain it has something to do with the '>' character. How would one redirect this output to /dev/usb/lp0 with subprocess?
To make sure the output is redirected properly, you need to set shell to True and pass a single string:
subprocess.call("png2pos -c example_2.png > /dev/usb/lp0", shell=True)
Otherwise ">" is treated as a program argument.
I do not have your tool installed, so I cannot really test here. But had an issue with redirecting output from a console application using python before. I had to redirect it using the command itself, not via the shell (as you are trying)
with open("/dev/usb/lp0", 'wb') as output_str:
subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)
This question already has answers here:
Can't find any info on Python's read() method (python 2.7)
(2 answers)
Closed 7 years ago.
I'm learning python from LPTH.
In exercise 15 in study drills, I'm supposed to know what read() does using pydoc; However when I try to do so with, python -m pydoc read(), I get an error like this.
an expression is expected after << ( >>
at line : 1 character 23
python -m pydoc read ( <<<< )
*category info : parser error : (:), parentcontainsErrorRecordException
* FullyQualifiedErrorID: ExpectedExpression
I don't understand what I did wrong.
I used the same way for: raw_input,os,open, but apparently I am doing something wrong with read().
You probably want this:
python -m pydoc read
But that will give you: no Python documentation found for 'read'
In what context are you trying to use read()... Example: to open a file and read and see associated docs, use
python -m pydoc file
This question already has answers here:
Running shell command and capturing the output
(21 answers)
Closed 8 years ago.
I want to be able to ping a server, and instead of just being returned with 0 or 1, I want to be able to see what you would see if you ran it in terminal your self, for example be able to see how many milliseconds it took for the packet to reach the host and get back.
I am using Python 2.7 and am on Linux, which is why in my code I use -c and not -n. This is the code I am using:
import os
response = os.system("ping -c 1 8.8.8.8)
print response
You should use subprocess.check_output:
https://docs.python.org/2/library/subprocess.html#subprocess.check_output
This question already has answers here:
read subprocess stdout line by line
(10 answers)
Closed 21 days ago.
My Python script will run a bunch of shell scripts that output either 200 SOLUTIONS_REVISION or 400 SOLUTIONS_REVISION when run. The 200 in the output indicates success and the 400 indicates failure.
How can I capture these "returned" strings as strs in the Python code, for further processing?
If you're going to run the command from your Python script then you want to look at subprocess with its stdout arguments. If you're going to run both that script and the Python script from a separate shell script then you want to pipe from one to the next and then read from sys.stdin.