Cookies with httplib
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.
import httplib
conn = httplib.HTTPConnection( "www.example.com" )
Headers = {"Cookie" : "Fish=Cod"}
conn.request("GET", "/fishfinder.html", None, Headers )
response = conn.getresponse()
data = response.read()
How hard can it be?
[
Thu, 01 May 2008 18:36] |
[
/python] |
#
Easy security blanket
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.
[
Mon, 19 Jun 2006 13:32] |
[
/python] |
#
Projects
This is a quick round up of the various projects I'm hosting here. There is a brief summary of each project with links to the original article, and the code.
Python
All my Python posts.
Wherever possible my code is GPL 2 licensed. If you need to use my code under a different license please ask, I may be amenable to persuation or payment.
[
Thu, 30 Mar 2006 21:52] |
[
] |
#
More Delicious tinkering
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.
[
Wed, 14 Sep 2005 12:55] |
[
/python] |
#
Apache++
I've spotted a couple of interesting C++ projects being run under the
Apache Software Foundation's
banner. Log4cxx
and stdcxx.
The stdcxx project is based on Rogue Wave Software's
implementation of the C++ Standard Library, which was donated to the
Apache Software Foundation earlier
this Summer and is currently being incubated in the Apache Incubator.
Having used Rogue Wave's non-standard library tools in the past I've got
mixed feelings about this, but having another good implementation of the
C++ Standard Library as open source should help all C++ developers.
Log4cxx is C++ port of the ubiquitous Log4j
project. Love it or hate it, good easy-to-use logging can be a life saver
for a developer. If you haven't already got a good, generic, and most
importantly sane in-house logging library Log4cxx is worth a good look.
It's also worth a look if you've got an existing system, after all like
the Boost libraries
why waste your time re-inventing the wheel when good, efficient, proven
code already exists?
[
Tue, 16 Aug 2005 20:52] |
[
/cpp] |
#
Python Quickies...
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!
Also a couple of nice things from Matt's PyS60 wiki page,
the Series60
tagged items from Code Snippets and
Simon Judge's Python freeware,
three handy little PyS60 apps - where's the source Simon? :-)
I've really got to sit down and tinker with PyS60 again, roll on the
26 hour day and the 9 day week...
[
Mon, 15 Aug 2005 23:12] |
[
/python] |
#
Del.icio.us tickering, step one
More
del.icio.us tinkering.
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.
My code is available here:
pydelicious.py
As per xmltramp and Roberto's original my code is GPL licensed. I
like to think of my contribution as standing on the toes of giants...
Progress reports will follow
on the wiki.
[
Mon, 21 Mar 2005 20:32] |
[
/python] |
#
Traffic Cams on Your Phone
Russ posted about a Flash Lite application for Series 60 that displays recent images from New York City traffic cameras on your phone. Not to be outdone Christopher Schmidt had a quick play at writing a similar app in Nokia's Series 60 Python, the result is this Traffic Cam Proof Of Concept written in about 1 hour 20 minutes!
It's a nice app, but not being in New York it's of little use to me, fortunately the BBC's Travel News page has a load of traffic cams, so I grabbed the BBC's list of London traffic cams and hacked up a London variant of Chris's app. You can find my version here - http://feetup.org/code/py/traffic.lon.py.txt
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.
[
Sat, 19 Feb 2005 12:44] |
[
/python] |
#
Are you blacklisted?
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.
[
Thu, 10 Feb 2005 22:19] |
[
/python] |
#
More Nokia Python
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.
[
Thu, 23 Dec 2004 16:33] |
[
/python] |
#
Christmas comes early..
for us mobile Pythonista's anyway!
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.
First step is porting
WikiLite
to my phone :-)
[
Wed, 22 Dec 2004 12:55] |
[
/python] |
#
Are All Almonds Bitter?
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
few
times,
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.
[
Fri, 02 Jul 2004 12:42] |
[
/python] |
#
The C++ Source
Given that
Guru of the Week
has gone pretty quiet it's hard to find consistant high quality
sources of C++ information these days on the web.
The C++ Source
is a new peer-reviewed, online journal for the C++ community.
The advisory board are hardly waht I'd regard as my peers, being
headed by
Bjarne
Stroustrup
and comprising the following luminaries: David Abrahams,
J. Stephen Adamczyk, Andrei Alexandrescu,
Matthew Austern, Pete Becker,
Walter Bright, Steve Clamage,
Greg Colvin, Jim Coplien,
Stephen Dewhurst, Howard Hinnant,
Bjorn Karlsson, Andrew Koenig,
Scott Meyers, Thomas Plum,
Dan Saks, Jerry Schwarz,
Jeremy Siek, Herb Sutter,
Matthew Wilson, Leor Zolman.
Erm, can I say "wow!"?
Best of all, there's an
RSS feed
too.
[
Wed, 23 Jun 2004 13:14] |
[
/cpp] |
#
Soup Kitchen
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.
[
Fri, 28 May 2004 12:53] |
[
/python] |
#
Symbian Python
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
#mobitopian
Enki 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.
[
Wed, 05 May 2004 11:18] |
[
/python] |
#
When is false always true?
When you're using a mixed mode .Net C++ program apparently!
If your managed C++ code calls an unmanaged virtual method
that returns a bool you will only ever see a return value of true.
The Code Project
documents
this astonishing "feature"
in .Net - truly scary stuff, just the sort of thing that'll
make you pull your hair out when debugging.
[
Mon, 29 Mar 2004 13:37] |
[
/cpp] |
#
WikiLite
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:
import BaseHTTPServer
import CGIHTTPServer
def run(server_class=BaseHTTPServer.HTTPServer,
handler_class=CGIHTTPServer.CGIHTTPRequestHandler):
server_address = ('', 8000)
handler_class.cgi_directories = ['']
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
run()
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!
[
Tue, 16 Mar 2004 22:05] |
[
/python] |
#
ACCU - whoops!
I just renewed my
ACCU
(the Association of C and C++ Users) subscription,
I'd nearly forgotten about it!
If you're even half serious about C++ you'd be daft not to join
these guys. The website has a huge number of
book reviews
and the
mentored developers programmes
are great for personal development. Their really good mailing lists
(some open to non members) are a great resource too.
Possibly the best bit is the two magazines every two months,
both written by members, one's a good general magazine
covering all levels of C and C++, book reviews, some Java,
some Python and general ACCU business. The other magazine
Overload
is superb, written by some of the top C++ developers in the World
(did I mention that Bjarne was a member?), and is full of
articles on leading C++ usage and design.
[
Mon, 06 Oct 2003 13:44] |
[
/cpp] |
#