title: python - socket download image
body: <p>Good morning, community.</p>
  
  <p>After hours of google I'm starting this thread. 
  I'm trying to download a picture from www with the python socket module. 
  I'm getting the source of the picture with "recv()" and writing it in binary mode to a file.
  Splitting the content from header with "answer.split("image/jpeg")" does not solve the problem.</p>
  
  <p>Heres my code:</p>
  
  <pre><code>import socket
  
  socket.setdefaulttimeout(30)
  
  HOST = 'ict.de'   
  PORT = 80         
  
  def makestr(s):#create paket-string from list
      rs = ""
      for x in range(0,len(s)-1):
              rs+=s[x]+"\r\n"
      rs+="\r\n"
      return rs
  
  def sendcap(sdata):
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((HOST, PORT))
      s.send(sdata)
  
      recvd=""
      while True:
          data = s.recv(1024)
          if not data: print " Done "; break
          recvd += data
      data = recvd.split("image/jpeg")[1]
      s.close()
  
      return data
  
  xy = []  
  xy.append("GET /fileadmin/ict/images/Presse/Download-Button_1700x1000.jpg HTTP/1.1")
  xy.append("Host: www.ict.de")
  xy.append("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101      Firefox/19.0")
  xy.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  xy.append("Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3")
  xy.append("Accept-Encoding: gzip, deflate")
  xy.append("DNT: 1")
  xy.append("Connection: keep-alive")
  data = sendcap(makestr(xy))
  f = open("logo.jpg","wb")
  f.write(data)
  f.close()
  </code></pre>
  
  <p>data in plain text is something like this (without header,split at image/jpeg) "ÿØÿá5ExifMM*bj(1r2Ž‡i¤Ð ü€' ü€'Adobe Photoshop CS5 Windows2012:03:05 13:37:44  ¤ è&amp;(. ÿHHÿØÿíAdobe_CMÿîAdobed€ÿÛ„         ÿÀ^ "ÿÝ ÿÄ?       3!1AQa"q2‘¡±B#$RÁb34r‚ÑC%’Sðáñcs5¢²ƒ&amp;D“TdEÂ£t6ÒUâeò³„"</p>
  
  <p>looks like binary-data.</p>
  
  <p>When I'm trying to open the file with windows picture show, it gets an error, that the file is corrupted.</p>
  
  <p>Hope somebody is able to help me.</p>