Friday, December 21, 2012

Fedora 17: Open port for TCP connection

To open a port for TCP connection you have to edit /etc/sysconfig/iptables and append the following line before the COMMIT line

-A INPUT -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

And then restart the iptables service by executing

service iptables restart

*EDIT*
I created this post before knowing that you could actually use iptables directly as below :P :

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

You should list your iptables rules first to check whether there's any DROP rule. If your new rule comes after a rule that DROPs any connection on you target port, the port you're trying to open will stay closed. If there's a DROP rule, use -I (insert) instead:

# iptables -I INPUT <rulenum> -m state --state NEW -m tcp -p tcp --dport <PORT> -j ACCEPT

where <rulenum> is the rule index in the chain. and <PORT> is your target port.

Friday, December 14, 2012

Python: Fun with sockets (remote desktop)

Server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#!/usr/bin/python

import os
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)

client, addr = s.accept()

cmd = s.recv(1024)
try:
    os.system(cmd)
    client.sendall("OK")
except:
    client.sendall("FAIL")
client.close()

Client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(sys.argv[1])

data = s.recv(32)

print data
s.close()
print "Received: '%s'"%data

Wednesday, November 28, 2012

Web programming with python and apache: Webserver index page (index.py)

To use python in a web server instead of php, you need to enable cgi-script in your httpd.conf using
AddHandler cgi-script .cgi .py
and you use
ScriptAlias /cgi-bin/ "/your/own/dir/for/cgi-bin/"
to tell apache the script the location of the scripts to be executed.

But if you want the script to handle the webserver index page as index.html, index.php does, follow the following steps:
  1. Edit your root directory settings to execute cgi scripts and handle .py files by writing:
    <Directory /your/root/dir>
      Options +ExecCGI
      AddHandler cgi-script .cgi .py
    </Directory>
  2.  Then set your DirectoryIndex as index.py as so:
    DirectoryIndex index.py

 

index.py "Hello, World!"

#!/usr/bin/python

import cgitb
cgitb.enable()

print "Content-Type: text/html;charset=utf-8\r\n" #tells the respond type to the client
print """<html><head><title>FooBarBaz</title></head>
<body>Hello world!</body>
</html>
"""

Saturday, September 29, 2012

Gtk+ tips: Set value of properties in C

In C, if you want to set the value of certain property of a Gtk+ object, you can do it like so
gtk_<type>_set_<property> (GTK_<TYPE> (var_name), <value>);
For example, to set the title of a GtkWindow to "Hello World", do would write
gtk_window_set_title (GTK_WINDOW (window), "Hello World");

This is fine, but what if you want to set the value of a bunch of properties, say for GtkWindow; "app-paintable", "default-width", "default-height", "resizable", and "modal", most beginner would write:

GtkWidget *window = gtk_window_new ();
gtk_widget_set_app_paintable (window), TRUE);
gtk_window_set_default_size (GTK_WINDOW (window), 100,100);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
gtk_window_set_modal (GTK_WINDOW (window), TRUE);

Too many characters to type (229 characters excluding spaces and newlines).

I prefer the GObject way  to do this task. The following is the code which does the same thing as the code above, but by using g_object_set:

GtkWidget *window = gtk_window_new ();
g_object_set (G_OBJECT (window),
        "app-paintable", TRUE,
        "default-width", 100, "default-height", 100,
        "resizable", FALSE,
        "modal", TRUE,
        NULL); //the arguments must end with NULL

You have to type much less characters, only 130 characters.  You can also remove the G_OBJECT macro from the code above. Moreover, your code would look more beautiful (in my eyes at least). :)

Friday, September 14, 2012

Fedora 17: Slow recreate volatile files and directories during boot

One of the reason for this is that there's a broken entry in /etc/fstab. Removing the entry would solve the problem and booting would be faster.

Saturday, July 7, 2012

Chord viewer? Music Viewer?



I created this out of boredom. Everything is drawn using cairo in vala. Now I'm not sure what to do with it. :/

Maybe I should learn about MIDI and then make a tablature editor out of this. :D






Tuesday, June 12, 2012

GTK3: How to create menu button?

In Gtk3, we are provided with GtkMenuToolButton GtkMenuToolButton to show a dropdown menu from a button, but this toolbutton consists of two parts: a button and an additional button that popups a menu when toggled. The first button is useless when we plan to use GtkMenuToolButton as a menu button, thus requires us to find new solution.

There are several methods to create menu button:
  1. Connect the "clicked" or "toggled" signal of a button or toggle button to a callback that pops up a menu. For this method, we need to create a custom positioning function, which is to me a tedious work.
  2. Customize the GtkMenuToolButton by getting it's child and remove the button part as well as replacing the GtkArrow with another widget.
  3. Using GtkMenuBar.
This article only covers the 3rd solution.

Convert GtkMenuBar into Menu Button

This is the simplest way to create a menu button. Before this, I used the second method to come up with a menu button until I updated my Gnome installation to version 3.4 and found out Epiphany 3.4['s] way of implementing this. I don't know why am I so stupid to come up with that method? :(

What the developer did, is just changing the style class of the GtkMenuBar into button style. That's all. As easy as that! >.<

Code (in python)
menub = Gtk.MenuBar()
onlyitem = Gtk.MenuItem()
onlyitem_child = Gtk.Button(label = "Menu")
onlyitem.add(onlyitem_child)
 
menu = Gtk.Menu()
mitem1 = Gtk.MenuItem(label = "Item 1")
mitem2 = Gtk.MenuItem(label = "Item 2")

menu.insert(mitem1, 0)
menu.insert(mitem2, 1)
onlyitem.set_submenu(menu)
menub.insert(onlyitem, 0)

menub.get_style_context().add_class("button") #this is the key line.

**UPDATE**
Since Gtk+ 3.6, a native menu button widget, GtkMenuButton has been added.

Saturday, June 9, 2012

GtkGrid example

I've just upgraded my Fedora 16 installation to Fedora 17 which includes gtk3-3.4. I was working on some hobby project—written in genie—of mine before the upgrade. After upgrading, I tried to compile the source code, but got deprecation errors for GtkHBox and GtkVBox. So I checked devhelp, and was suggested to use GtkBox instead. So I read GtkBox documentation, then I found out that it's also will face the same fate as it's brothers. GtkGrid became the new way. I kinda don't like this change. But anyhow, life must goes on.

Continuing on, GtkGrid is similar to GtkTable, but unlike GtkTable, it stores the child's expand and margin property instead of having its container to give allocation to it. Seems to me to be GtkBuilder or GtkUIManager (I never used this before) friendly.
 
In genie, If you want to create a box with 3 buttons with the following layout
[<button1:!expand><<<<button2:expand>>>>><button3:!expand>]

the old way to do this is
...
var
     box = new HBox(false, 0)
     b1 = new Button.with_label("button1")
     b2 = new Button.with_label("button2")
     b3 = new Button.with_label("button3")

box.pack_start(b1, false, true, 0)
box.pack_start(b2, true, false, 0)
box.pack_start(b3, false, true, 0)
...

now, with GtkGrid, you had to do like this

...
var
     grid = new Grid
     b1 = new Button.with_label("button1")
     b2 = new Button.with_label("button2")
     b3 = new Button.with_label("button3")

b2.expand = true
grid.attach(b1, 0, 0, 1, 1)
grid.attach(b2, 0, 1, 1, 1)
grid.attach(b3, 0, 2, 1, 1)
...

Tuesday, May 15, 2012

GtkClutterEmbed the Pythonic Way!

The following is a simple clutter-gtk program in python that has an animatable widget. The script is for Gtk+3.0 not Gtk+2.0.

#!/usr/bin/python


from gi.repository import Gtk, Clutter, GtkClutter


storedWidth = 0
storedHeight = 0


def on_clicked(b, actor):
    global storedWidth, storedHeight
    if actor.get_width() != actor.get_stage().get_width():
        storedWidth = actor.get_width()
        storedHeight = actor.get_height()
        width = actor.get_stage().get_width()
        height = actor.get_stage().get_height()
    else:
        width = storedWidth
        height = storedHeight
        
    actor.animatev(Clutter.AnimationMode.EASE_OUT_BOUNCE,
                   500, ["width", "height"], [width, height])
    
GtkClutter.init([])


bttn = Gtk.Button("Foobar")
txtv = Gtk.TextView()
scwn = Gtk.ScrolledWindow()
scwn.add(txtv)
scwn.show_all()
bact = GtkClutter.Actor(contents = scwn)
embd = GtkClutter.Embed()
stge = embd.get_stage()
col = Clutter.Color()
col.from_string("#000")
stge.set_color(col)
vbox = Gtk.VBox()
wndw = Gtk.Window(width_request = 400, height_request = 300)


stge.add_actor(bact)
vbox.pack_start(embd, True, True, 0)
vbox.pack_start(bttn, False, True, 0)
wndw.add(vbox)


wndw.show_all()


bttn.connect("clicked", on_clicked, bact)
wndw.connect("destroy", lambda w: Gtk.main_quit())


Gtk.main()