Thanks all for solving my first question.But there is a final problem.
#import socket module
import sys
import httplib
from socket import *
serverName = sys.argv[1]
serverPort = int(sys.argv[2])
filename = sys.argv[3]
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
clientSocket.send("GET /filename")
while True:
data = clientSocket.recv(1024)
if not data:
break
print data,filename
clientSocket.close()
FInally, i can't receive the content with the certain filname.I think the point is in "/" How can i solve it??
-
1What do you receive?Robᵩ– Robᵩ2013年10月16日 13:14:57 +00:00Commented Oct 16, 2013 at 13:14
2 Answers 2
Have you tried using the python requests package?
Either way, you have a problem here:
clientSocket.send("GET /filename")
Should (at the very least) be:
clientSocket.send("GET /%s" % filename)
When you write filename inside the string, it will not evaluate that to the variable filename instead, you need to use string formatting
answered Oct 16, 2013 at 13:04
Inbar Rose
43.8k24 gold badges91 silver badges137 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Is this program communicate with HTTP server?
Then, it should send CR+LF twice to correctly denote the end of HTTP header.
clientSocket.send("GET /{}\r\n\r\n".format(filename))
answered Oct 16, 2013 at 13:04
falsetru
371k69 gold badges770 silver badges660 bronze badges
2 Comments
user2877854
I am connected to socket server.But can u tell more about \r\n\r\n? It is about the knowlege of http potocol?
falsetru
@user2877854,
\r\n\r\n is there to separate http header from http body. w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.1 lang-py