I needed to do a simple http get in the other day, the only catch was that I had to send a cookie. My first thoughts were that this might be tricky, but in practise it's a doddle. Below is a simplistic example passing a cookie (called "Fish", with a value of "Cod") using Python's httplib.
Back in May del.icio.us announced some changes to their api, in a post curiously titled Security blanket.
It took all of two minutes to update my linkblogging script (a different url, and https rather than http), source code for which can be found in the usual location: pydelicious.py
One gotcha for Pythonistas is that the default win32 build of older versions of Python doesn’t have any ssl support, I’ve tried Robin Dunn’s drop-in replacement for _socket.pyd and it seems to work fine.
You may have noticed that I’ve given my linkblog entry generator a slight revision, the output should be a little more informative with links to the tags used, and a link to the other people linking to each item.
As before, it’s GPL licensed and the source code is available as: pydelicious.py
One odd thing I encountered with pytextile was when I tried being too clever and used a definition list instead of nested unordered lists; definition list are pretty easy to do use in pytextile, but they appear to having a flaw in implementation. Their syntax is simple enough, for example
.bc dl. Monty Python:A British tv comedy series which first aired in 1969
would generate:
Monty Python
A British tv comedy series which first aired in 1969
It uses the colon character to split the items up, however if you try to use a link in a definition list things start getting strange. The format for a link is "link text":http://example.com and the definition list code spots the colon and split the link over two items! There’s probably a simple way around this but it’s not in my textile skillset yet.
Here's a few nice Python quickies, I know I could have used del.icio.us to
tag them and dump them here, but I wanted to add some more comments than
del.icio.us really allows, so they're here instead.
First off, the invaluable Python Grimoire
a vital resource for someone like me, someone who writes Python too
infrequently and sporadically and hence forgets some of the syntax and the
best Pythonic ways to do routine tasks. Rui's been maintaining it
for a while, but he's now relaunched it in TiddlyWiki
format, this is a great way to read and browse the grimoire, I love it!
As you've probably seen I've got stage one working successfully,
which creates a daily post of my previous day's del.icio.us items.
I've achieved this using a lightly modified version of
Roberto De Almeida's
python script
pydelicious
- lightly modified because I don't have the libxml2 module to hand, so
I've substituted Aaron Schwartz's
xmltramp instead.
I run this script with cron every morning - the
del.icio.us api
is UTC/GMT based so that suits me quite nicely. The script creates a
new text file containing my links (marked up with textile), which pyblosxom
renders as a new blog post.
In keeping with the RAD theme, I timed how long it took me to create, 30 minutes to find the url's of the cameras and put them into a text file, 20 minutes to hack these urls into the code, and I'd got a functional app. Now it's not that pretty; the location names are all in uppercase, and the image sizes from the BBC site (320x240 and around 40k bytes) mean that you need to wait around 10 seconds for the picture to load (at a cost of about 10p for the average UK GPRS user), and zoom in to get the best view, but as a proof of concept it's great.
Update: I've been pointed at Frixo an interesting site that seems to cover the UK's traffic with live updates every 3 minutes. No traffic cams that I can see yet, but lots of useful info (traffic, weather etc), with maps of jams etc. Good stuff, highly recommended.
How do you check if an ip address is blacklisted by one of the various
DNS Blackhole Lists?
It's sort of easy, you reverse the address (say it was 1.2.3.4), and
append the blacklist's address (say blacklist.example.net), and then
do a dns lookup (of 4.3.2.1.blacklist.example.net). If the address is not
found, chances are the blacklist hasn't heard of them, otherwise they're
probably scum.
I've talked
about blocking these parasites before, so here's a chunk of code I use in
a few places to spot them. It's called
blacklist.py
and I think it's simple enough to use:
import blacklist
if blacklist.blacklisted("1.2.3.4") == 1:
print("scum")
else:
print("ok")
Update: Thanks to
Blackie
for
spotting the typo
in the above example (5 lines of code and I still make a mistake).
Now fixed.
It looks like people haven’t been slow to get using Nokia’s Python project, and Matt Croydon has been collating projects and news on his Python for Series 60 wiki page.
Seeing as there’s no central clearing house(other than Forum Nokia) for Nokia Python projects right now, I reckon this is as good a focal point as any.
Nokia have today publicly released
their long awaited
Python for Series 60
Symbian smart phones. It'd be nice to see it released on Series 80 and
Series 90 phones as well as the mass market
Series 60 devices, I guess
that's Nokia's next step.
It's certainly something to keep me busy over Christmas. I need to
catch up with the product since I haven't touched it for a little while,
and the release candidate has many more features than the last version of
the beta I used. And with Ewan chasing me for a few
All About Symbian
articles on Python, I'll certainly be busy.
Matt
and
Russ
have posted some of their thoughts about
Amaretto,
Nokia's port of Python for the Series 60 platform.
I agree whole-heartedly with their views, it's a great looking
product, but it's currently a little incomplete (in a non-major
but frustrating way), and we're all pretty much tied with NDAs
to stop us engaging in our usual discussions and batting around
of code.
It's no secret that I'm on the eval programme, I've
mentioned
it before
a
fewtimes,
and I was impressed by the helpful emails I received from a couple
of Nokia folks earlier this week after
Planet Python
bizarrely grabbed my old mentions of the programme.
I still really hope that Nokia will release their Python source
once they're happy with it as a basic product, to
allow it to flourish as an open source project.
This would make it far easier for the series 60 port
to keep track with mainstream Python releases and fixes,
and would also avoid the risk Amaretto becoming an oddball
faux-Python that couldn't run the vast range of publicly
available Python software.
Ever had to parse some tag soup HTML? An unpleasant
job at the best of times.
Beautiful Soup
claims to have solved the problem (mostly),
and leaves you free to spend your time doing
useful stuff with the extracted data rather than
reinventing the wheel continuously. Beautiful Soup
is written in Python, and works with Python 1.5.2
or later. It's all in one file, and Python licensed.
Nice looking stuff, I've got to do some more parsing
and scraping, so I'm going to give this a try.
I'm evaluating
Amaretto
the Nokia port of
Python for
the Series 60 Symbian platform, my initial impressions are mixed.
Partially great joy and partially frustration, now I'm not allowed to
say exactly what functionality is provided, but being as it's a technology
preview you can assume that it's currently a little incomplete. There's
a basic core to the product that looks good and sane, but the bits around
that are currently a little sketchy.
This incompleteness is rather frustrating, but not as annoying as the
current release only running on series 60 V2, of which the 6600 is the
only target phone released. Out of the handful of Symbian phones I've
easy access to, none are 6600s, so I'm stuck with using Amaretto
in the emulator on the PC, which is handier for writing code, but
rather limiting.
Anyway, given all the hype of Amaretto, a Python port for UIQ
(SonyEricsson P800, P900, Motorola A920, A925, A1000, Benq P30,
P31 et al) has sneaked out from sometime
#mobitopianEnki Boehm.
Classic stuff and very much in the tradition of Python on
Symbian/Epoc, where there have been a number of ports in the past
from lone hackers scratching their itches. A trend started by
Duncan Booth, and continued by
a few others
The most recent port being
2.2.1
by Olaf Flebbe.
I needed a lightweight standalone Wiki for tinkering around and
writing a few project notes, unfortunately most of the
WikiWikiClones
needed Apache or IIS, and
I wanted something self contained and lite.
So, exactly how lite? 25 lines of Python,
I guess that's probably lite enough!
How did I do that? Pretty easily actually, Python's
CGIHTTPServer
module did all the heavy lifting,
Sean Palmer's
minimal Wiki
WyPy
did the WikiMagic, and a minor tweak of the example
code for the
BaseHTTPServer
module (shown below) glues it together:
Install instructions: Put the above code (I called my copy MyCgi.py)
and wypy.py in a directory, create a subdirectory named w, run
MyCgi.py and point your browser at http://localhost:8000/wypy.py
Minor niggles so far are that I can't get the 11 line version of
WyPy to work so I've had to resort to using the (not very) bloated
18 line version, and that WyPy isn't as fully featured as say
MoinMoin. I've
also had to change two places in wypy.py that use "wypy"
as an href to "wypy.py" as Windows isn't smart enough
to autorun Python scripts without a bit of a nudge.
I'm looking at the 11 line version, and that should be fixable,
plus as I'm considering using this same Wiki setup on various
devices including my Psion 5mx and hopefully my N-Gage (when
Nokia finally release their Python port), featherweight code
is a good thing!