Tag Archives: python

Selecting Python version in MacPorts

List installed versions:

port select --list python

Select the preferred version, e.g.

sudo port select --set python python27

For more info:

port help select
Advertisements

Configure Tor to use a node in a specific country

Create a configuration file, say “torrc”

(Default configuration file and a sample file location in Mac OS X Lion: /usr/local/etc/tor/)

Include the following lines in the configuration file:


ExitNodes {TR}
StrictExitNodes 1

Run Tor from the Terminal:


tor -f /Users/admin/Documents/Python/MyProject/torrc

To test it with Python:


from selenium import webdriver
import socket
profile=webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
browser=webdriver.Firefox(profile)
browser.get("http://www.iplocation.net/")

Useful link for checking Tor status, Tor servers with country information, etc:

http://torstatus.blutmagie.de

See also:

Using Python urllib2 via Tor

If socks is not installed:

sudo easy_install SocksiPy-branch

Then the following code to check the new IP:

import socks
import socket
def create_connection(address, timeout=None, source_address=None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)

socket.socket = socks.socksocket
socket.create_connection = create_connection

import urllib2

try:
    # Check IP
    print urllib2.urlopen("http://almien.co.uk/m/tools/net/ip/").read()
    # Change and check user-agent which is normally Python-Urllib2
    req = urllib2.Request("http://whatismyuseragent.dotdoh.com/")
    req.add_header('User-agent', 'Mozilla 5.10')
    res = urllib2.urlopen(req)
    print res.read()
except urllib2.HTTPError, e:
    print e.code
except urllib2.URLError, e:
    print e.args

Saving Screenshots of Webpages using Python+webkit2png

Example for scanning and saving Google Mars images:

import os 
for j in range(120):
  for i in range(180):
    os.system('python webkit2png -F -W 1280 -H 800 -D img -o test%(imgnum)05d "http://www.google.com/mars/#lat=%(lat).6f&lon=%(lon).6f&zoom=8&map=visible"'  % {'imgnum': j*180+i, 'lon': 2*i-180, 'lat': j-60} )

Python ile Türkçe heceleme

Girilen metni önce kelimelere ayırıyor, sonra da hecele() fonksiyonu ile heceleri arka arkaya ekrana yazıyor. Girdiyi metin dosyasından okur hale gelirse hece istatistiğinde kullanılabilir.

#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys

def lowercase(ch):
	return {
	'İ':u'i',
	'I':u'ı',
	'Ç':u'ç',
	'Ğ':u'ğ',
	'Ş':u'ş'	
	}.get(ch, ch.lower())
			
def sesli(ch):
	ch = lowercase(ch)
	if ch in [u'a', u'e', u'i', u'ı', u'o', u'ö', u'u', u'ü']:
		return True
	else:
		return False

def hecele(str):
	index=0
	length=len(str)
	while sesli(str[index]) == False and length>index+1:
		index=index+1
	try:
		if sesli(str[index+1]):
			print str[0:index+1],
			hecele(str[index+1:])
		elif length>index+2:
			if sesli(str[index+2]):
				print str[0:index+1],
				hecele(str[index+1:])
			elif length>index+3:
				if sesli(str[index+3]):
					print str[0:index+2],
					hecele(str[index+2:])
				else:
					if str[index+1:index+4] in [u'str', u'ktr', u'mtr', u'nsp']:
						#print "istisna!.."
						print str[0:index+2],
						hecele(str[index+2:])
					else:
						#print "üç sessiz, normal kural"
						print str[0:index+3],
						hecele(str[index+3:])
			else:
				print unicode(str),
		else: 
			print unicode(str),
	except:
		print unicode(str),
		return
			
		

inp = raw_input("hecelenecek kelime: ").decode(sys.stdin.encoding)
inp = inp.strip('."\',;')
words = inp.split()
for word in words:
	hecele(word)


Selenium with Tor+Firefox (Mac)

Install Selenium:

sudo easy_install selenium

If Homebrew is not installed:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"

Install Tor:

brew install Tor

Run Tor:

tor

In another terminal window, run the python code to open Firefox+Tor and save a screenshot via Selenium:

from selenium import webdriver
profile=webdriver.FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)
browser=webdriver.Firefox(profile)
browser.get("http://yahoo.com")
browser.save_screenshot("/Users/admin/Pictures/screenshot.png")
browser.close()

See also:

Image resize to portrait height or landscape width using Python & PIL

from PIL import Image

filename = "car_.jpg"
im = Image.open(filename)

ratio = float(im.size[0]) / float(im.size[1])
print ratio
if ratio > 1:
	target_size=(500, int(500/ratio))
else:
	target_size=(int(500*ratio),500)
im2 = im.resize((target_size), Image.NEAREST)  # use nearest neighbour
im2.save("resized/"+filename)
print filename + "resized to " + repr(target_size) + " and saved."

Unicode Text Statistics in Python

import nltk
import codecs
f = codecs.open('/path/to/input.txt', encoding='utf-8')
raw = f.read()
raw = raw.lower()
tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)
fdist1=nltk.FreqDist(text)
voc = fdist1.keys()
output_file = codecs.open('output.txt', 'w', 'utf-8')
index = 0
for term in voc[:500]:
 output_file.write(term)
 output_file.write(' ')
 output_file.write(unicode(fdist1[term]))
 output_file.write('\n')
 index+=1
output_file.close()