Skip to content
Advertisement

Python call to PHP – malformed header from script Bad header: Array

Very strange – I’ve been using a python script that scrapes data and then passes it to a php-script – Worked like charm for weeks, until today something happened.

I got the following message from apache2 errorlog:

   malformed header from script 'pytest.py': Bad header: Array

So it seems to be derived from this python script, the php-script is called but there is a server 500 error and the error log tells about a malformed header from python

python script

#!/home/john/mydir/my_venv/bin/python3

# enable debugging
import cgitb
from bs4 import BeautifulSoup                                       
from urllib.request import urlopen, Request
import os

cgitb.enable()

print ("Content-type: text/htmlrnrn")

url = "https://www.xxxx.."


soup = BeautifulSoup(urlopen(url).read())

stockNames = []
stockClose = []
stockHigh = []
stockLow = []
stockTurnover = []

for tag in soup.select('td[data-title="Namn"]'):    
   name = tag.get_text(strip=True, separator='n')
   stockNames.append(name)  

for tag in soup.select('td[data-title="Senast"]'):
   close = tag.get_text(strip=True, separator='n')     
   stockClose.append(close)


os.system('php /var/www/html/omx/dataparser.php %s %s'%(stockNames, stockClose))

the php script (snippet)

  <?php


if (isset($argc)) {

    unset($argv["0"]); // remove first argument i.e. filename

    ...


}

Any Idea whats wrong, why this suddenly happened? Could a packetsniffer be in place to see whats going on?

Advertisement

Answer

I solved the problem by flushing the outputstream buffer

More precisely I changed the printfunction from:

print (“Content-type: text/htmlrnrn”)

to:

print (“Content-type: text/html”, end=”rnrn”, flush=True)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement