stdout

How to limit the size of subprocess stdout and stderr in python

若如初见. 提交于 2019-12-05 16:52:05
I need to run applications submitted by users. My code looks like: def run_app(app_path): inp = open("app.in", "r") otp = open("app.out", "w") return subprocess.call(app_path, stdout=otp, stdin=inp) Now since I have no control over what users will submit, I want to restrict the size of the output of the application. Other things like trying to access unauthorized system resources and abusing of CPU cycles are being restricted by apparmor rule enforcement. The maximum time allowed to run is being handled by parent process (in python). Now a rogue application can still try to flood the server

pipe python logging stdout stream output to grep

回眸只為那壹抹淺笑 提交于 2019-12-05 15:07:47
I knew the reason for this a while back but I have since forgotten and 5 mins of googling hasn't revealed the answer. I have written a python script which has two handlers. One for files and one for streams. Everything works as I want. Now, I wanted to quickly grep for something in the output that was being printed to the terminal but piping the script's output through grep doesn't appear to be working in that all of the output still get's printed to the terminal. I am using unix and python 2.7 This is probably a duplicate question but I can't find the answer. Here's my setup of the logging

C++ print value of a pointer

可紊 提交于 2019-12-05 13:20:45
I have an array of double pointers, but every time I try do print one of the values the address gets printed. How do I print the actual value? cout << arr[i] ? cout << &arr[i] ? they both print the address Does anyone know? If it's really an array of (initialized) double pointers, i.e.: double *arr[] = ... // Initialize individual values all you need is: cout << *arr[i]; cout << *(arr[i]) will print the value. cout << *(arr[i]); If "arr" is declared as double* arr[..]; Then you would use: cout << *(arr[i]) 来源: https://stackoverflow.com/questions/2485565/c-print-value-of-a-pointer

Should I set stdout and stdin to be unbuffered in C?

本小妞迷上赌 提交于 2019-12-05 13:01:52
Because of stdin and stdout buffering sometimes printf , scanf and getchar are not executed. I usually flush output buffer using fflush(stdout) but code can become very unreadable because of that. If I set stdin and stdout unbuffered using setbuf(stdin, NULL) and setbuf(stdout, NULL) will I make my program perform better or worse? Making stdin or stdout completely unbuffered can make your program perform worse if it handles large quantities of input / output from and to files. Most I/O requests will be broken down as system calls on a byte by byte basis. Note that buffering does not cause

Add new line in text file with Windows batch file

半世苍凉 提交于 2019-12-05 12:30:58
问题 I have a text file which has more than 200 lines in it, and I just want to add a new line before line 4. I'm using Windows XP. Example text file before input: header 1 header 2 header 3 details 1 details 2 After output: header 1 header 2 header 3 <----- This is new line ----> details 1 details 2 回答1: I believe you are using the echo Text >> Example.txt function? If so the answer would be simply adding a "." (Dot) directly after the echo with nothing else there. Example: echo Blah echo Blah 2

How to redirect C-level streams in Python in Windows?

喜你入骨 提交于 2019-12-05 11:34:44
Eli Bendersky has explained thoroughly how to " Redirecting all kinds of stdout in Python ", and specifically Redirecting C-level streams, e.g. stdout of a shared library (dll). However, the example is in Linux and does not work in windows, mainly due to the following lines: libc = ctypes.CDLL(None) c_stdout = ctypes.c_void_p.in_dll(libc = ctypes.CDLL(None), 'stdout') How can we make it work in Windows? I found the answer buried in Drekin's code . Based on that, I made a small change to Eli Bendersky's example : Update: This code has been tested on Python 3.4 64-bit on Windows and Python 3.5

How to test print statements?

♀尐吖头ヾ 提交于 2019-12-05 10:27:05
问题 You want to write unittest -cases for a function like that: def test_me(a): for b in c: print do_something(a,b) At first I thought about just collecting the outputs of do_something in a string and then returning it, to print and test the whole output together. But it's not always convinient because such loops could cause your buffer string to get very big, depending on the circumstances. So what can you do to test the output, when it is printed and not returned? 回答1: print prints to sys

Python subprocess with stdout redirect returning an int

我只是一个虾纸丫 提交于 2019-12-05 10:20:20
I am trying to read out data from a set of print statements in a C++ program that is being run using a subprocess. C++ code: printf "height= %.15f \\ntilt = %.15f \(%.15f\)\\ncen_volume= %.15f\\nr_volume= %.15f\\n", height, abs(sin(tilt*pi/180)*ring_OR), abs(tilt), c_vol, r_vol; e; //e acts like a print Python code: run = subprocess.call('Name', stdout = subprocess.PIPE, env={'LANG':'C++'}) data, error = run.communicate() However instead of getting the data, all I am getting is a single int, the exit code, either a 0 or an error code. Of course, python then tells me "AttributeError: 'int'

Output not printing without fflush(stdout)

*爱你&永不变心* 提交于 2019-12-05 10:12:31
I don't understand why sometimes I need to use fflush() and sometimes not. My program is segfaulting at the moment and I am debugging it with print statements. When a program segfaults, does stdout not flush its buffer automatically? I don't understand why sometimes I need to use fflush() and sometimes not. Sometimes the stdio buffers are flushed sometimes they aren't. For example simply including a "\n" in the printed stuff will typically flush it (because stdout is by default line-buffered when attached to a terminal). When a program segfaults, does stdout not flush its buffer automatically

Node.js's python child script outputting on finish, not real time

有些话、适合烂在心里 提交于 2019-12-05 10:05:34
I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output. Eventually this will be used for a temperature sensor so for now I have a dummy script which prints temperature values every few seconds: Thermostat.py import random, time for x in range(10): print(str(random.randint(23,28))+" C") time.sleep(random.uniform(0.4,5)) Here's a cut down version of the server: Index.js var sys = require('sys'), spawn = require('child_process').spawn, thermostat = spawn('python', ["thermostat.py"]), app = require('express')(), http = require(