1
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))

#mysock.send('GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n')
mysock.send('GET http://www.py4inf.com/code/intro-short.txt HTTP/1.0\n\n')

while True:
    data = mysock.recv(512)
    if ( len(data) < 1 ) :
        break
    print data
mysock.close()

I get 404. I tried replacing \n\n with \r\n in GET command then I get nothing.

HTTP/1.1 404 Not found Server: nginix

abestrad
  • 898
  • 2
  • 12
  • 23
magnolia04
  • 11
  • 2
  • 1
    Bad request ? Try the following format: "`GET / HTTP/1.0\r\nHost:http://www.py4inf.com/code/romeo.txt\r\n\r\n`". Why do you want to download the file via GET ? For example, try this code: https://stackoverflow.com/questions/27241804/sending-a-file-over-tcp-sockets-in-python And what about the `urllib2` module ? It's more comfortable. Or do you need to use the `socket` module exclusively ? – s3n0 Mar 10 '19 at 10:19
  • To expand on @s3n0's comment, you're trying to get a file called `http://www.py4inf.com/code/intro-short.txt` which I doubt is what you want. You probably meant `GET /code/romeo.txt`. But I second s3no's suggestion: unless you have a compelling reason to use sockets, you're better off using `urllib2`. – Federico klez Culloca Mar 10 '19 at 11:05
  • Ok. Actually, I could do the same thing using urllib. link = 'http://www.py4inf.com/code/intro-short.txt' f = urllib.urlopen(link) myfile = f.read() print(myfile). Thanks. – magnolia04 Mar 10 '19 at 17:45

0 Answers0