Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

J2ME - getIPdevice

// Retrevie IP device

package org.socketdemo;

import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SocketDEMO extends MIDlet implements CommandListener
{
	protected SocketDEMO midlet = this;
	
	private Alert info;
	
	protected void destroyApp(boolean value) throws MIDletStateChangeException
	{
		notifyDestroyed();
	}

	protected void pauseApp()
	{

	}

	protected void startApp() throws MIDletStateChangeException
	{
		new Thread()
		{
			public void run()
			{
				SocketConnection socket = null;
				
				try
				{
					socket = (SocketConnection) Connector.open("socket://193.204.114.233:13");
					
					socket.openInputStream();
					
					info = new Alert("Info", "Current IP: " + socket.getLocalAddress() + "\nPort: " + socket.getLocalPort(), null, AlertType.INFO);
					info.setTimeout(Alert.FOREVER);
					info.setCommandListener(midlet);
					
					getDisplay().setCurrent(info);
				}
				catch(Exception error)
				{
					info = new Alert("Info", "Current IP: N/A\nPort: N/A", null, AlertType.INFO);
					info.setTimeout(Alert.FOREVER);
					info.setCommandListener(midlet);
					
					getDisplay().setCurrent(info);
				}
				finally
				{
					if(socket != null)
					{
						try
						{
							socket.close();
						}
						catch(Exception error)
						{
							
						}
					}
				}
			}
		}.start();
	}
	
	protected Display getDisplay()
	{
		return Display.getDisplay(this);
	}

	public void commandAction(Command cmd, Displayable dsp)
	{
		if(cmd == Alert.DISMISS_COMMAND)
		{
			try
			{
				destroyApp(true);
			}
			catch(MIDletStateChangeException error)
			{
			
			}
		}
	}
}

PyS60 - BabelFish

// Translate from language A to language B
// code not complete but it works

import urllib

####################################################################################### <BabelFish>
class BabelFish(object):
    
    def translate(self, lang, message):
        
        try:
            url = urllib.URLopener()
        
            query = urllib.urlencode({
                                      'doit':'done',
                                      'intl':'1',
                                      'lp':lang,
                                      'tt':'urltext',
                                      'urltext':message
                                      })
        
            responde = url.open('http://babelfish.altavista.com/tr', query).read()
        
            start = responde.find('<div style=padding:10px;>') + 25
            stop = responde.find('</div>', start)
        
            return responde[start:stop]
        
        except Exception, error:
            return '-' + str(error)
####################################################################################### </BabelFish>

####################################################################################### <BabelFishUI>
from graphics import *

import appuifw
import e32

class BabelFishUI(object):
    
    def __init__(self):
        
        self.__lock = e32.Ao_lock()
        self.__img = Image.new((176, 144))
        self.__language = 'it_en'
        self.__textUI = None
        
        appuifw.app.exit_key_handler = lambda:self.__lock.signal()
        
        appuifw.app.title = u'BabelFish v1.0'
        appuifw.app.body = self.__canvas = appuifw.Canvas(redraw_callback=self.updateScreen)
        
        appuifw.app.menu = [(u'Translate', lambda:self.__translateUI()), (u'About', lambda:appuifw.note(u'BabelFish: v1.0", "Created by\nWhite Tiger\n<Z-TEAM@Libero.it>', 'info')), (u'Exit', lambda:self.__lock.signal)]
        
        self.updateScreen(None)
        
        self.__menuMain = appuifw.app.menu
        self.__bgMain = appuifw.app.body
        
        self.__lock.wait()
    
    def updateScreen(self, rect):
        
        self.__canvas.blit(self.__img)
    
    def __back(self):
        
        appuifw.app.menu = self.__menuMain
        appuifw.app.body = self.__bgMain
        
        appuifw.app.set_tabs([u'Back'], lambda x:None)
    
    def __translateUI(self):
        
        self.__textUI = appuifw.Text()
                
        appuifw.app.menu = [(u'Translate', lambda:self.__translate()), (u'Language', lambda:self.__setLanguage()), (u'Clear', lambda:self.__textUI.clear()), (u'Back', lambda:self.__back())]
                   
        appuifw.app.body = self.__textUI
                
    def __setLanguage(self):
        
        resp = appuifw.selection_list([u'italiano-inglese', u'inglese-italiano', u'inglese-francese', u'francese-inglese', u'inglese-tedesco', u'tedesco-inglese',
                                       u'francese-italiano', u'italiano-francese'], 1)
        
        if resp == 0:
            self.__language = 'it_en'
        elif resp == 1:
            self.__language = 'en_it'
        elif resp == 2:
            self.__language = 'en_fr'
        elif resp == 3:
            self.__language = 'fr_en'
        elif resp == 4:
            self.__language = 'en_de'
        elif resp == 5:
            self.__language = 'de_en'
        elif resp == 6:
            self.__language = 'fr_it'
        elif resp == 7:
            self.__language = 'it_fr'
            
    def __translate(self):
        
        babel = BabelFish()
        
        resp = babel.translate(self.__language, self.__textUI.get())
        
        if resp[0] == '-':
            self.__textUI.set(unicode(resp[1:]))
        else:
            self.__textUI.set(unicode(': ' +self.__textUI.get() + '\n: ' + resp))
                
        appuifw.note(u'Translate', 'conf')
####################################################################################### </BabelFishUI>

if __name__ == '__main__':
    
    BabelFishUI()

Python - My External IP Address

// My external ip address

import urllib

url = urllib.URLopener()
resp = url.open('http://myip.dk')
html = resp.read(114)

end = html.find("</title>")
start = html.find("IP:") + 3

print html[start:end].strip()

Python - Change user-agent

// Cambiare user-agent con urllib

import urllib

class AppURLopener(urllib.FancyURLopener):

	version = 'Nokia6630/1.0 (2.3.129) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1'

urllib._urlopener = AppURLopener()


// Cambiare user-agent con urllib2

import urllib2

headers = { 'user-agent':'Nokia6630/1.0 (2.3.129) SymbianOS/8.0 Series60/2.6 Profile/MIDP-2.0 Configuration/CLDC-1.1',
		    'keep-alive':'300',
		    'content-type':'application/x-www-form-urlencoded'
		  }
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS