0

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??

asked Oct 16, 2013 at 13:01
1
  • 1
    What do you receive? Commented Oct 16, 2013 at 13:14

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I am connected to socket server.But can u tell more about \r\n\r\n? It is about the knowlege of http potocol?
@user2877854, \r\n\r\n is there to separate http header from http body. w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.1

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.