Getting extra b' while calling external python script.

Hi
Getting extra b’ while calling external python script.

Form Template:-

  {% csrf_token %}
 Input Text:
  <input type="text" name="param" required><br><br>
  {{data_external}}<br><br>
  <input type="submit" value="Execute External Python Script">
</form>

Views

import requests
import sys
from subprocess import run,PIPE

def external(request):
inp= request.POST.get(‘param‘)
out= run([sys.executable,‘//mnt//e//work//djnago_testing//test.py‘,inp],shell=False,stdout=PIPE)
print(out)
return render(request,‘home.html‘,{‘data1‘:out.stdout}

test.py
#/usr/bin/python3
import sys
import datetime

time=datetime.datetime.now()
output=“Hi %s current time is %s” % (sys.argv[1],time)
print(output)

Getting Output:-
b’Hi Everyone current time is 2022-11-24 12:57:27.871323\n’

You can see b’ is coming from nowhere. Please help.

The b is not “coming from nowhere”, it’s an indication that what you’ve got is a bytestring. Note that this indicator is a result of printing that output and is not part of the string itself.

Yes, I tried to use
b=XYZ
output=b.decode(‘utf-8’)

But still getting this error.

Is there any other way to handle it…?

I missed one more point.
My goal is to run the external Python script while clicking submit tab. Also, the input field is to accept password which will be 1st parameter in external script.
Thanks

What “error”? I see no error here.

(Side note: showing a couple lines of code outside the context in which they’re being used doesn’t help. Please post the complete function.)

Not any error, even after using decode to UTF, I’m getting same “b’” in output.

Please post the complete function that you’re currently using.

Please check the details in url

I don’t see where you have the decode method being used in this.

You only need to post your updated test.py file, if that’s what you’re concerned about.

Also, for the long-term viability of the information being posted here, please post the code here - don’t link to external sources.

In beginning, I tried to post all code here only but it was not coming in proper format thats why I used google doc.

Now, I’m not getting any output except b"

test.py

#/usr/bin/python3
import sys
import datetime

time=datetime.datetime.now()
result=“Hi %s current time is %s” % (sys.argv[1],time)
output=result.decode(‘utf-8’)
print(output)

Ahh - when posting code here, enclose each file’s contents between lines of three backtick - ` characters. That means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep the code properly formatted. (You can also do this with templates, error messages, html, and anything else you don’t want the forum to format or interpret.)

Try running your test.py script directly from the command line - does it work? Does it throw an exception?

If this only fails when being executed by your view, you might also want to capture stderr from the process and output that as well.

result.decode was throwing syntax error while running through command line. So, I replaced it with # -- coding: utf-8 --

#/usr/bin/python3
import sys
import datetime

time=datetime.datetime.now()
result="Hi %s current time is %s" % (sys.argv[1],time)
# -*- coding: utf-8 -*-
print(output)

I’m getting correct output while running script through cli

$ python3 test.py Everyone
Hi Everyone current time is 2022-11-24 20:29:00.748037
$ 

But I’m still getting extra b’ and \n through django web page

b'Hi Everyone current time is 2022-11-24 14:58:26.119814\n' 

There are two separate issues here:

  • The print statement is adding the newline character. You can get rid of it using the end argument to print. But, if you correct the second issue, it shouldn’t be needed.

  • The output is coming back to your view as a bytestream. It’s in your view that you need to do the decode of the returned string from the script.

Sorry, for bugging you. Actually, I’m new to django

def external(request):
	inp= request.POST.get('param')
	out= run([sys.executable,'//Path//test.py',inp],shell=False,stdout=PIPE)
    # -*- coding: utf-8 -*-
	return render(request,'logs.html',{'data02' :out.stdout})

I tried 2-3 option, but still I’m not able to find the solution

  1. It doesn’t make any difference
    # -*- coding: utf-8 -*-
  2. Script doesn’t worked
from django.template import Template
     t2 = Template('This is a string template.')

This is a comment - perhaps a directive to an IDE or editor, but functionally it doesn’t do anything for you.

If you need to convert the bytestring to a string you would call decode on the bytestring to convert.

Your function is rendering logs.html - what does that template look like?

HTML Template

	<form action="/external/" method="post">
	  {% csrf_token %}
	 Input Text:
	  <input type="text" name="param" required><br><br>
	  {{data_external}}<br><br>
	  <input type="submit" value="Submit">
	  
	</form>

I feel, do we have something in HTML, where we can define that take input data as str instead of Bytes? Or not sure if we have some different options

The issue isn’t in either your template or your script.

The issue is in using the run command and passing data through the command parameters and stdout. Those interfaces are, by definition, bytestreams. Python has no way of passing or receiving a Python String externally.

So it doesn’t matter what you do - what you send to your script is going to be a bytestream and what you get back is going to come back as a bytestream. It’s up to you to convert that into a string where needed.

In views, I tried all below three but last 2 show syntax error…1st one no difference in output

  • Using [decode()]
  • Using [str()]
  • Using [codecs.decode()]

If you’re looking for specific assistance, you’ll need to provide the code where you tried to use the decode function.

Views

import sys
from subprocess import run,PIPE
import codecs

def external(request):
	inp= request.POST.get('param')
	result= run([sys.executable,'//Path//test.py',inp],shell=False,stdout=PIPE)
    out = codecs.decode(result)
	return render(request,'logs.html',{'data02' :out.stdout})

Here I tried all 3

  • Using [decode()]
  • Using [str()]
  • Using [codecs.decode()]

Output is:-
b’Hi Everyone current time is 2022-11-27 07:40:25.297318’

Want to remove first b…which is in bold.