Thread Tools
This thread is privately moderated by Jack Crossfire, who may elect to delete unwanted replies.
Aug 09, 2014, 01:16 AM
Registered User
Jack Crossfire's Avatar
Thread OP
Discussion

Language of the day: python


So you want a server side script to download an image, scramble it so Sprint can't degrade the quality, & forward it to a client. Simply forwarding the image through https would be ideal, but free hosting of https is extremely rare. There are many free servers with varying amounts of cgi support. Few have been around longer than 5 years, yet they all advertize being around since the dawn of internet time. Their cgi support varies from nothing to just Perl, to Python but no javascript or C. Sourceforge.net allows C but no sockets. x10hosting was the winner, with Python.

Code:
#!/usr/bin/python

import socket
import zlib
import sys
import string

if len(sys.argv) < 2:
	print "Content-type:text/html\n"
	print "Need a URL"
	sys.exit(0)

argArray = string.split(sys.argv[1], '/')

host = argArray[2]
path = "/%s" % (string.join(argArray[3:], '/'))

#print host
#print path
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))

s.send("GET %s HTTP/1.1\r\n\
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n\
Host: %s\r\n\r\n" % (path, host))

buffer = [];
buffer2 = [];
gotLength = 0
length =  0
while 1:
	try:
		fragment = s.recv(0x1000)
		if fragment:
			buffer.append(fragment)
			buffer2 = ''.join(buffer)
			if(gotLength == 0):
				index1 = string.find(buffer2, "Content-Length: ")
				index2 = string.find(buffer2, "\r\n\r\n")
				if(index1 >= 0 and index2 >= 0):
					gotLength = 1
					length = string.atoi(buffer2[index1:].split(": ")[1].split("\n")[0])
					length += index2 + 4

			if(gotLength == 1 and len(buffer2) >= length):
				break;

		else:
			break
	except:
		pass

buffer = buffer2

index1 = buffer.find("\r\n\r\n")
index1 += 4

buffer2 = zlib.compress(buffer[index1:len(buffer)], 9)

print "Content-type:application/octet-stream\n"

#print ("%d %d" % (len(buffer), len(buffer2)))
print buffer2
That did the business. It was horribly inefficient & fragile. There are many libraries for downloading URLs, but nothing ubiquitous & every version of Python is incompatible, so a bare socket interface won. It then used basic zlib compression to pass the data through the proxy. The Java client has a similarly simple zlib inflater, using the Inflater object. The compression doesn't really compress anything for an image.

As many people love Python as those who loved Perl before it, but it's just semantics. and instead of &&, indentation instead of {} , the need for : started to get a bit ridiculous.

Unfortunately, the idea of getting rid of Commie cast is still busted, since you need Skype nowadays for job interviews & Skype has unknown performance over LTE.
Last edited by Jack Crossfire; Aug 09, 2014 at 03:19 AM.
Sign up now
to remove ads between posts
Aug 09, 2014, 07:02 AM
Registered User
Or in PHP:

Code:
<?php
header('Content-Type: application/octet-stream');
ob_start("ob_gzhandler");
readfile($url);


Quick Reply
Message:
Thread Tools