1

Question, so i need to send \n in a packet now, but python treats it as a EOL. How can i tell it to treat \n as raw text and part of the packet exactly?

logcom = "LOGON\n Protocol-Version: 2.0\n App-type: Windows x86\n Operator: PC_CLIENT\n name: " + pal_user + "\n capabilities: 4\n"

I need to send all of it in one packet, but it treats \n as EOL when the packet has multiple lines, but not when its just LOGON\n

socket.sendall("'LOGON\n ^ SyntaxError: EOL while scanning string literal

The error^

6
  • 2
    It would help us in helping you if you described your problem a bit more or if you posted some code. Are you using tcp/udp sockets or RAW? Commented Nov 2, 2012 at 0:38
  • im using a TCP socket, and the first part of the packet is LOGON\n , but it treats \n as part of code, but it needs to be send in the packet CODE: 'logcom = "LOGON\n Protocol-Version: 2.0\n App-type: Windows x86\n Operator: PC_CLIENT\n name: " + pal_user + "\n capabilities: 4\n" ' Commented Nov 2, 2012 at 0:40
  • I am assuming you're using socket.sendall('LOGON\n'), is that correct? If so, then the sendall function sends the whole string as is, along with whitespace characters such as \n. Commented Nov 2, 2012 at 0:41
  • Could you post logcom for us to see? Commented Nov 2, 2012 at 0:43
  • 1
    It is not a socket error, it's a syntax error. I suspect a missing closing quote. You need to post your code. Commented Nov 2, 2012 at 1:03

2 Answers 2

2

You almost surely have a missing closing quote on a line. Just use it like this:

logcom = "LOGON\n Protocol-Version: 2.0\n App-type: Windows x86\n Operator: PC_CLIENT\n name: " + pal_user + "\n capabilities: 4\n" socket.sendall(logcom) 

If you want to split it over several lines in your source code, as you have in your question, you should write it like this with triple quotes:

logcom = """LOGON Protocol-Version: 2.0 App-type: Windows x86 Operator: PC_CLIENT name: {} capabilities: 4 """.format(pal_user) socket.sendall(logcom) 
Sign up to request clarification or add additional context in comments.

Comments

1

If you need to send the '\' in the '\n', then all you need to do is escape the character. Thus, you should be sending:

logcom = "LOGON\\n Protocol-Version: 2.0\\n App-type: Windows x86\\n Operator: PC_CLIENT\\n name: " + pal_user + "\\n capabilities: 4\\n" 

If you need to send the data with the newlines included, I would try using socket.send in a loop.

7 Comments

Or put r before the string literal.
well i need to send the entire \n in the packet, but it gets treated as a EOL no matter what
like the packet structure is 'LOGON\n Protocol-Version: 2.0\n App-type: Windows x86\n Operator: PC_CLIENT\n name: " + pal_user + "\n capabilities: 4\n'
Is the problem that it's only sending a piece at a time? Shouldn't you just keep reading?
here lets say socket.sendall("'LOGON\n ^ SyntaxError: EOL while scanning string literal i have to send it all in ONE packet and include the \n part IN the packet, i dont know how to explain it much better
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.