Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
\n not working
#1
Why does the following code work in my CLI, but not on my local Apache web server?

#!C:\Python310\python.exe
print ("Content-type: text/html\n\n")
str1="Hi there, Chris."
str2="It works!"
print(str1 + "\n" + str2)
the web server output is "Hi there, Chris. It works!" (w/o the quotation marks)
Reply
#2
'\n' is not recognized as a new line (AKA a line break). For HTML, you need to use <br>

Or you could create it with CSS
DeaD_EyE likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
Please explain exactly what isn't working and show all relevant code. There's nothing there that really has anything to do with a web server (other than a string that looks like an HTTP header, but it's just a string). Don't make us guess, please.
Reply
#4
(Jun-15-2022, 07:48 PM)WouldWerker Wrote: Why does the following code work in my CLI, but not on my local Apache web server?

Your Command Line Interface sees the \n as newline character and go to the next line.
The Web Browser doesn't. The Web Browser want to get HTML Content. Try out what rob101 said.
A simple \n should work in <pre>\n</pre> tags, but they are used to displaying monospaced fonts (good for code), but all other tags like p or div don't interpret the newline character. Instead, <html-tags> are interpreted, and they can be nested. Replace \n with <br />.


In addition: If you want to program a web application, this is the wrong way.
You need a framework like Flask or Django and a Templating System like Jinja2 to create from Data + Template -> HTML-Page with data

They way you did it, is from the 90s. In early days, code was written like you did.
WouldWerker likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thank you all for your help. I have just started learning Python, so I'm a complete newby. My goal is to use it for data analytics including website dev. So I'll include Flask in my learning time.

Again, thank you all.

PS. I apologize for not being more descriptive with my issue. I'll do better with the next question :).
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020