Ready to bring some flavor of C++ into python? If you like cout, cin in C++ and also a python programmer, you’d definitely like this snippet.
import sys
class ostream:
def __init__(self, file):
self.file = file
def __lshift__(self, obj):
self.file.write(str(obj));
return self
cout = ostream(sys.stdout)
cerr = ostream(sys.stderr)
endl = 'n'
x, y = 'Printing', 'like C++'
cout << x << " " << y << endl
I found this and a lot of other cool stuffs from Peter Norvig’s blog. I highly recommended subscribing to this blog.
While working on unicode based characters in python, you’ll often come across this type error message (this cost me a while to fix).
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
This will happen if you do not set your character encoding in your python file to UTF-8. First you need to make sure the first few lines of your programm looks like this.
#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-
This enables you to write unicode characters in your source code. But this does not enable you to print them in console and you’ll still keep getting the same error I previously mentioned.
To solve this you need to add the following code in your /usr/lib/python2.5/sitecustomize.py file (This might change depending your installed python version)
import sys;
sys.setdefaultencoding('utf-8')
The interesting part is, if you try to do that in your source file, it won’t work, I kept getting this error
AttributeError: 'module' object has no attribute 'setdefaultencoding'
I’m not sure why python does this, but a reasonable explanation would be not to change the character encoding in runtime, that could pose vulnerability to the system.
utf-8 isn’t default in python, maybe they’ll fix this in python 3.0
Directory listing is a great security risk for websites. Anyone can see and download contents from the directory. It may reveal your website’s login and database information. It will definitely make the server side scripts public which puts website’s security at a stake. It may also reveal private contents like images or multimedia files to public.
Now, there are a number of ways you can prevent this from happening. Let me describe one by one -
- Change Apache configuration file to disallow indexing. To do this, open
httpd.conffile. Probable location of this file is/etc/apache2/. Now change the line
Options Indexes FollowSymLinks Includes ExecCGI
to
Options -Indexes FollowSymLinks Includes ExecCGI
Restart the apache server.
- Now you may not have access to the apache configuration file if you’re using a shared hosting. In this case, you can disallow directory listing with the help of .htaccess file. .htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.Create a file named
.htaccessin/var/www/. Note that the file has no name and only an extension htaccess. It has to be named exactly like this. Otherwise, it wont work. Add this line in the file
Options -Indexes
and save it. Now try seeing the directory listing again on the browser. You should get a403 Error (Access forbidden). If you still see the listing, then it means that your .htaccess file is ignored by apache server. This happens when .htaccess file is disallowed from httpd.conf file. To enable .htaccess files, replace the following in httpd.conf file
AllowOverride None
with
AllowOverride All
or
AllowOverride Options
- Finally, if you dont have the authority to follow the above mentioned remedies, there’s still a solution for you. Simply, put an empty
index.htmlfile in every directory. This way, no one will ever be able to see the content of the directory. Rather he or she will see a blank page.
Regarding hyperlinks, there has been a significant change from Actionscript 2 to Actionscript 3.
In Actionscript 2, the following line of code would have sufficed to open a window in your default browser for this blog.
getURL("http://www.muktosoft.com/");
But in Actionscript 3, you need to instantiate an URLRequest then call navigateToURL to (:) navigate to your target url. See the code snippet bellow.
import flash.net.navigateToURL; var req:URLRequest = new URLRequest("http://www.muktosoft.com"); navigateToURL(req);
Code explained :
First of all, import the required class from flash.net pacakge.
Now instantiate a URLRequest object with the target url provided in constructor, for our example the target is muktosoft.com.
Then call the public function navigateToURL (provided in flash.net package) to, offcourse, navigate to that url.
You can also control whether the new page should load in the same window or in a different one by providing a second parameter. For example, to open the target page in a new window, you have to put “_blank” as the second parameter.
navigateToURL(new URLRequest("http://www.muktosoft.com"), "_blank");
The possible values for the second parameter and their meanings are
The second parameter actually lets you tell on which browser window or HTML frame to display the document indicated by the request parameter. You can enter the name of a specific window or use one of the following values:
* “_self” specifies the current frame in the current window.
* “_blank” specifies a new window.
* “_parent” specifies the parent of the current frame.
* “_top” specifies the top-level frame in the current window.
Hope you find this useful.
Search
News
Blog
- Fetching data from multiple tables in one MySQL query – Part 2
- Fetching data from multiple tables in one MySQL query
- Let’s Go A Hunting
- Java Applet and Javascript message passing
- The Right-Click Menu in Flash
- Stroke hinting
- Python: C++ style cin, cout in Python
- Python: Working in Unicode
- Website Security : Directory Listing Issue
- Hyperlink in actionscript 3


June 25, 2009 in
