[Pythonmac-SIG] printing <cr><lf>, in interpreter vs. IDE - both wrong?

Leo leoweb@cox.net
Sat, 08 Mar 2003 23:43:17 -0800


Hi.  I just joined this list as I'm doing work on Python on the Mac. I hope
this forum is suitable for these questions.  Couldn't find the Mac FAQ, and
dejanews didn't quite answer this (although I found other issues with
<CR><LF>) and couldn't find anything relevant in Release notes or other
docs.


Problem: I get a blank line where there should be text, when I print a
simple (HTTP request) string containing \r\n\r\n.  Similarly, I get blank
lines when I print the response I get from the remote server (which also
contains several <cr><lf> pairs).  For debugging purposes (see testcrlf),
(1) when running in the interpreter, I get a blank line instead of a string,
when the string ends in \r\n ; (2) Python_IDE does not substitute blank line
for the string but prints an additional blank line - and why is running via
the IDE giving different output than via Interpeter?

I'm using Mac distribution 2.2.2 (on Mac OS 9.2.2 / G4 with 300+ MB's of
RAM.)


THE DEBUG CODE:

def testcrlf():
    print "==="

    #one blank line between two lines
    #with <CR> <LF> sequence (NOTE ORDER of \r, \n)
    #Note disappearing first line in output,
    #in PythonInterpreter but not in PythonIDE
    msg = "One Line Followed by\r\n\r\nOneBlankLine"
    print msg    
    print "==="

    #one blank line between two lines
    #with <LF> <CR> sequence (NOTE ORDER of \r, \n)
    #Output is correct on screen,
    #but order would not be proper for HTTP request
    msg = "One Line Followed by\n\r\n\rOneBlankLine"
    print msg    
    print "==="

    

THE OUTPUT ON INTERPRETER:
    ===
    
    
    OneBlankLine
    ===
    One Line Followed by
    
    OneBlankLine
    ===

THE OUTPUT ON Python_IDE:
    ===
    One Line Followed by
    
    
    
    OneBlankLine
    ===
    One Line Followed by
    
    
    
    OneBlankLine
    ===


For what it's worth, here's the ultimate use of all the above:

def httpget(theUrl):
    # using code from "Python Cookbook"
    
    remote_host=theUrl
    remote_port=80
    
    msg = "GET / HTTP/1.0\r\n\r\n"
    
    # Create a socket

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Connect to the remote host and port
    sock.connect ((remote_host, remote_port))

    #Send a request to the host
    sock.send(msg)
    
    print msg
    print "Here is the response"
    
    #Get the host's response, no more than, say, 1024 bytes
    response_data = sock.recv(1024)
    
    print response_data

    #Terminate
    sock.close()