Hopefully we will find more companies looking for Python programmers in Bangladesh in neat future.
Street Gamers Network
2 weeks ago
explore the power of Python...
def get_html_source(url, referer = '', data = 0, cj = 0, retry_counter = 0):
if retry_counter > 0:
print 'Trying Again...'
if retry_counter > 3:
print 'Could not get source from url:', url
return '', ''
try:
if cj:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
else:
opener = urllib2.build_opener()
opener.addheaders = [('Referer', referer),
('Content-Type', 'application/x-www-form-urlencoded'),
('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14'),
('Accept-Encoding', 'gzip,deflate')]
if data:
# HTTP POST
usock = opener.open(url, data)
else:
# HTTP GET
usock = opener.open(url)
content = decode(usock) # I think I have already written the code of decode function
# in another post. If you can't find it, just leave a comment
# here and I shall post the code again.
usock.close()
return content, cj
except urllib2.HTTPError, e:
print 'The server couldn\'t fulfill the request. for url: ', url
print 'Error code: ', e.code
return get_html_source(url, referer, data, cj, retry_counter + 1)
except urllib2.URLError, e:
print 'We failed to reach a server.'
print 'Reason: ', e.reason
return get_html_source(url, referer, data, cj, retry_counter + 1)
import Image
import os, sys
# adjust width and height to your needs
width = 800
height = 600
for root, dirs, files in os.walk('./'):
for name in files:
filename = os.path.join(root, name)
if filename.endswith('jpg'):
print filename
imin = Image.open(filename)
imout = imin.resize((width, height), Image.BICUBIC)
imout.save(filename)
import urllib2
url = 'http://abc.com/dir/file1.mp3'
usock = urllib2.urlopen(url)
data = usock.read()
size = data.__len__() # size in bytes
size = size / 1024.0 # in KB (Kilo Bytes)
size = size / 1024.0 # size in MB (Mega Bytes)
...
data = usock.read() where the whole file is being read to get it's size! This solution came to my mind first. But soon I understood that the file size can be found from the http response header. Here is a much better solution:
import urllib2
url = 'http://abc.com/dir/file1.mp3'
usock = urllib2.urlopen(url)
size = usock.info().get('Content-Length')
if size is None:
size = 0
size = float(size) # in bytes
size = size / 1024.0 # in KB (Kilo Bytes)
size = size / 1024.0 # size in MB (Mega Bytes)
...
myList = list(set(myList))
>>> myList = [1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> myList
[1, 2, 3, 3, 2, 2, 4, 5, 5]
>>> myList = list(set(myList))
>>> myList
[1, 2, 3, 4, 5]
>>>
import urllib2
import cookielib
# create cookiejar to store cookie
cj = cookielib.CookieJar()
# IP:PORT
proxy_address = '66.98.208.8:3128' # change the IP:PORT, this one is for example
# create the proxy handler
proxy_handler = urllib2.ProxyHandler({'http': proxy_address})
# create opener
opener = urllib2.build_opener(proxy_handler, urllib2.HTTPCookieProcessor(cj))
# install the opener
urllib2.install_opener(opener)
# url to browse / visit
url = "http://www....com/" # change the url
req=urllib2.Request(url)
data=urllib2.urlopen(req).read()
print data
# now write the data to a text file
# create file handler
fh = open('page.txt', 'w')
data = "".join(data)
# write to file
fh.write(data)
# close the file handler
fh.close()
py2sis[sisfile] [--uid=0x12345678] [--appname=myapp]
[--presdk20] [--leavetemp]
F:\Nokia\Tools\Python_for_Series_60\py2sis>py2sis.exe
dse_stock_tracker.py dse.sis --uid 0x12345678
--appname=DSEStockPriceTracker