0

Hey I'm working on a python project using sockets. Basically I want to loop a connection to a host for user input. Here is what I'm trying:


while True:
 sock.connect((host, port))
 inputstring = "> "
 userInput = raw_input(inputstring)
 sock.send(userInput + '\r\n\r\n')
 recvdata = sock.recv(socksize)
 print(recvdata)

But when I loop the socket and try connecting to localhost on port 80 I get an error saying the transport endpoint is still connected or something like that. How can I eliminate that problem?

asked Mar 5, 2011 at 3:10

3 Answers 3

2

Call connect outside the while loop. You only need to connect once.

answered Mar 5, 2011 at 3:15
Sign up to request clarification or add additional context in comments.

Comments

1

Put the sock.connect outside of your while True.

answered Mar 5, 2011 at 3:15

1 Comment

Aye because you are not closing that socket, and yet trying to connect to it over and over again.
0

You only need to connect to the host once, then you can run your loop. Try:

sock.connect((host, port))
while True:
 inputstring = " > "
 # etc

If the body of your loop were to sock.close(), you would need to .connect() again at the top of the loop. But you probably don't want to disconnect and reconnect each time, so do the above.

answered Mar 5, 2011 at 3:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.