I'm using Python to write a simple script that will connect to a host/port using a socket. I know sockets can only be used once which is why I'm not closing the socket at all but when I connect to localhost on port 80 and try a simple command like GET / it works the first time but the second time I execute GET / or any other HTTP command, it doesn't print the output. Here is what I have
import socket
size = 1024
host = 'localhost'
port = 80
def connectsocket(userHost, userPort):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP socket
s.connect((userHost, userPort))
while(1):
input = raw_input("Command: ")
s.send(input + '\r\n\r\n') #Send command
r = s.recv(size) #Recieve output
print(r)
connectsocket(host, port)
I'd assume this would work but here is a sample output:
amartin@homebox:~$ python socketconn.py
Command: GET /
[BUNCH OF HTML CODE]
Command: GET /
Command:
As you can see it works for the first GET / but not for the second. How can I fix this?