1

i want to download a file from this url (http://justlearn.16mb.com/a.jpg) using python sockets only and i dont know how to do it as i am a novice in python.

Actually my main goal is to download files in half part using wifi connection and other half part using ethernet connection.

Thank you in advance for helping.

import os
import socket

tcpd = 'http://justlearn.16mb.com/a.jpg'
portd = 80
ipd = socket.gethostbyname('http://justlearn.16mb.com/a.jpg')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((tcpd,portd))



BUFFER_SIZE = 1024

with open('a.jpg', 'wb') as f:
    print ('file opened')
    while True:
        #print('receiving data...')
        data = s.recv(1024)
        #print('data=%s', (data))
        if not data:
            f.close()
            break
        # write data to a file
        f.write(data)

print('Successfully get the file')
s.close()
print('connection closed')     

2 Answers2

1

You might want to try something like this instead. I am unable to test it due to a proxy, but the example should help you in the right direction. Using sockets directly would make this unnecessarily difficult.

#! /usr/bin/env python3
import http.client


def main():
    connection = http.client.HTTPConnection('justlearn.16mb.com')
    connection.request('GET', '/a.jpg')
    response = connection.getresponse()
    if response.status != 200:
        raise RuntimeError(response.reason)
    with open('a.jpg', 'wb') as file:
        while not response.closed:
            buffer = response.read(1 << 12)
            if not buffer:
                break
            file.write(buffer)
    connection.close()


if __name__ == '__main__':
    main()

Here is another example that is shorter and uses the urlopen function from the urllib.request package instead. The code is simpler since the HTTP code is handled in the background instead.

#! /usr/bin/env python3
from urllib.request import urlopen


def main():
    with urlopen('http://justlearn.16mb.com/a.jpg') as source, \
            open('a.jpg', 'wb') as destination:
        while True:
            buffer = source.read(1 << 12)
            if not buffer:
                break
            destination.write(buffer)


if __name__ == '__main__':
    main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • Thanks , but then by using this urllib or http.client lib , i think i won't be able to force my program to use a specific network adapter like i have two internet connection one wifi and ethernet. i want to able to download using ethernet while i want to surf in browser using wifi – SAGAR AGRAWAL Sep 09 '16 at 14:58
  • @SAGARAGRAWAL How are you forcing a specific network adapter to be used in your code? – Noctis Skytower Sep 16 '16 at 20:10
  • i am thinking of using windows native netsh command to add specific route to specific destination ip from particular network interface or this s.bind(('192.168.0.1', 0)) s.connect(('...')) – SAGAR AGRAWAL Sep 25 '16 at 15:16
0

How big is the file? I'm assuming it's bigger than 1KB. When you write s.recv(1024), you are only receiving 1024 bytes. Try a bigger number as the parameter.

It really would be easier to use urllib.request, at least try and if you can't "force my program to use a specific network adapter", then never mind.

DarthVlader
  • 344
  • 3
  • 14