0

So, say I create a TCP socket in python, and the server I am authorizing to needs multiple headers sent at once to authorize, how would I go about this? As I am going to use socket.sendall to send them, then wait for the authorization handshake reply. After that, I need it to just wait for a data reply until it gets one, how would I do this also, because doesn't it close the socket if data is not received in a set amount of time? Is there some easy way to send this correctly or what? ~~Edit~~ My main question i guess is how to send a multi line packet? :)

3
  • 3
    Could you be more specific? Maybe you could include which protocol you're using? There's no such thing as a "header" as the socket level, only incoming and outgoing data. Regarding whether a socket would close if no data is received, that's not the case, TCP sockets can remain open for a very long time. Commented Nov 1, 2012 at 21:46
  • well, see lets say the server im sending too needs a length and a message id header send, over the tcp socket, how would i send a multi line message ? Commented Nov 1, 2012 at 22:02
  • If you're speaking a line-oriented TCP protocol, just put line-breaks in the string you send: your_socket.send( "line 1\nline 2" ) Commented Nov 2, 2012 at 0:24

1 Answer 1

1

A python socket of the type SOCK_STREAM, using the socket.sendall(data) function simply sends a string. Yep, data is just a string. Now, you can construct your own headers and lines and everything and put it in a string and tell the socket to send it all off towards the server.

Then just do socket.recv(buffer) where buffer is an integer of the number of bytes. And as Thomas Orozco said, tcp sockets can remain open for a very long time, so make sure the server gives a fairly prompt reply.

edit: Btw, TCP is a transportaion layer protocol, you can read up a bit more here : http://en.wikipedia.org/wiki/Transport_layer . Simply put, TCP will transport whatever you tell it to transport. In python, it's strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.