As Damon suggested; get wireshark (formerly called ethereal) it is typically the best place to start when debugging weird network issues.<br><br>cheers,<br><br>S<br><br><div class="gmail_quote">On Wed, Mar 11, 2009 at 3:37 PM, Damon McCormick <span dir="ltr"><<a href="mailto:damonmc@gmail.com">damonmc@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I've used a tool called <a href="http://www.charlesproxy.com/" target="_blank">Charles</a> for http, and <a href="http://www.wireshark.org/" target="_blank">Wireshark</a> is the standard for general protocol analysis. Both run on OS X.<br>
<font color="#888888"><br>-Damon</font><div><div></div><div class="h5"><br>
<br><div class="gmail_quote">On Wed, Mar 11, 2009 at 3:14 PM, Daryl Spitzer <span dir="ltr"><<a href="mailto:daryl.spitzer@gmail.com" target="_blank">daryl.spitzer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Thanks Benjamin. I should have wrote sooner to add that I've<br>
confirmed that curl -u works. So the problem is definitely with my<br>
Python basic HTTP authentication code.<br>
<br>
I also should have explained that I want to use urllib2 because (as<br>
Michael Foord wrote in<br>
<a href="http://www.voidspace.org.uk/python/articles/urllib2.shtml#proxies" target="_blank">http://www.voidspace.org.uk/python/articles/urllib2.shtml#proxies</a>),<br>
urllib2 will auto-detect proxy settings and use them; curl--as far as<br>
I know--doesn't auto-detect proxy settings. (So my code using query<br>
string parameters works fine at work through a proxy server and at<br>
home.)<br>
<br>
Also, I'd just like to learn why my code doesn't work!<br>
<br>
I guess I should take jj's advice and look at what is being sent over<br>
the wire. (And compare that to what's going over the wire with curl.)<br>
I've never done that before. Can anyone recommend a tool for that<br>
(preferably that works on Mac OS X)?<br>
<br>
--<br>
<font color="#888888">Daryl<br>
</font><div><div></div><div><br>
<br>
On Wed, Mar 11, 2009 at 2:07 PM, Benjamin Sergeant <<a href="mailto:bsergean@gmail.com" target="_blank">bsergean@gmail.com</a>> wrote:<br>
> Warning: quick and dirty solution with no error checking using curl and a<br>
> unix pipe.<br>
><br>
> - Benjamin<br>
><br>
> [bsergean@localhost bin]$ cat trim.py<br>
> #!/usr/bin/env python<br>
><br>
> import os, sys<br>
> from getpass import getpass<br>
> import xml.dom.minidom<br>
><br>
> if len(sys.argv) != 2:<br>
> print 'Usage: trim.py <url to shorten>'<br>
> sys.exit(0)<br>
><br>
> user = raw_input('username: ')<br>
> passwd = getpass('password: ')<br>
><br>
> # See <a href="http://tr.im/api" target="_blank">http://tr.im/api</a><br>
> url = '<a href="http://api.tr.im/api/trim_url.xml?url=%s&newtrim=yes" target="_blank">http://api.tr.im/api/trim_url.xml?url=%s&newtrim=yes</a>' % sys.argv[1]<br>
><br>
> # Warning: --silent don't show error messages<br>
> cmd = 'curl --silent --basic -u %s:%s ' % (user, passwd)<br>
> cmd += url<br>
><br>
> fd = os.popen(cmd, 'r')<br>
> response = fd.read()<br>
> print response<br>
> fd.close()<br>
><br>
> Doc = xml.dom.minidom.parseString(response)<br>
> N = Doc.getElementsByTagName("url")<br>
> shortened_url = str((N[0].childNodes)[0].data)<br>
><br>
> print 'shortened url:', shortened_url<br>
><br>
><br>
> [bsergean@localhost bin]$ python trim.py<br>
> Usage: trim.py <url to shorten><br>
> [bsergean@localhost bin]$ python trim.py<br>
> <a href="http://www.reddit.com/r/programming/" target="_blank">http://www.reddit.com/r/programming/</a><br>
> username: bsergean<br>
> password:<br>
> <?xml version="1.0" encoding="UTF-8"?><br>
> <trim><br>
> <status result="OK" code="200" message="<a href="http://tr.im" target="_blank">tr.im</a> URL Added."/><br>
> <url><a href="http://tr.im/hglC" target="_blank">http://tr.im/hglC</a></url><br>
> <reference>ZfCAq7qcyouYXwfR2WsuBR6v3V4q7L</reference><br>
> <trimpath>hglC</trimpath><br>
> </trim><br>
><br>
> shortened url: <a href="http://tr.im/hglC" target="_blank">http://tr.im/hglC</a><br>
><br>
><br>
><br>
><br>
><br>
><br>
> On Tue, Mar 10, 2009 at 11:51 PM, Shannon -jj Behrens <<a href="mailto:jjinux@gmail.com" target="_blank">jjinux@gmail.com</a>><br>
> wrote:<br>
>><br>
>> It may help to look at what is being sent over the wire.<br>
>><br>
>> -jj<br>
>><br>
>> On Tue, Mar 10, 2009 at 8:26 AM, Daryl Spitzer <<a href="mailto:daryl.spitzer@gmail.com" target="_blank">daryl.spitzer@gmail.com</a>><br>
>> wrote:<br>
>> > I'm playing around, trying to write some code to use the <a href="http://tr.im" target="_blank">http://tr.im</a><br>
>> > APIs (<a href="http://tr.im/api/" target="_blank">http://tr.im/api/</a>) to shorten a URL.<br>
>> ><br>
>> > After reading <a href="http://docs.python.org/library/urllib2.html" target="_blank">http://docs.python.org/library/urllib2.html</a>, I tried:<br>
>> ><br>
>> > TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>'<br>
>> > auth_handler = urllib2.HTTPBasicAuthHandler()<br>
>> > auth_handler.add_password(realm='<a href="http://tr.im" target="_blank">tr.im</a>',<br>
>> > uri=TRIM_API_URL,<br>
>> > user=USERNAME,<br>
>> > passwd=PASSWORD)<br>
>> > opener = urllib2.build_opener(auth_handler)<br>
>> > urllib2.install_opener(opener)<br>
>> > response = urllib2.urlopen('%s/trim_simple?url=%s'<br>
>> > % (TRIM_API_URL, url_to_trim))<br>
>> > url = response.read().strip()<br>
>> ><br>
>> > response.code is 200 (I think it should be 202). url is valid, but<br>
>> > the basic HTTP authentication doesn't seem to have worked, because the<br>
>> > shortened URL isn't in my list of URLs (at <a href="http://tr.im/?page=1" target="_blank">http://tr.im/?page=1</a>).<br>
>> ><br>
>> > After reading<br>
>> > <a href="http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly" target="_blank">http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly</a><br>
>> > I also tried:<br>
>> ><br>
>> > TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">api.tr.im/api</a>'<br>
>> > password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()<br>
>> > password_mgr.add_password(None, TRIM_API_URL, USERNAME, PASSWORD)<br>
>> > auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)<br>
>> > opener = urllib2.build_opener(auth_handler)<br>
>> > urllib2.install_opener(opener)<br>
>> > response = urllib2.urlopen('http://%s/trim_simple?url=%s'<br>
>> > % (TRIM_API_URL, url_to_trim))<br>
>> > url = response.read().strip()<br>
>> ><br>
>> > But I get the same results. (response.code is 200 and url is valid,<br>
>> > but not recorded in my account at <a href="http://tr.im/" target="_blank">http://tr.im/</a>.)<br>
>> ><br>
>> > If I use query string parameters instead of basic HTTP authentication,<br>
>> > like this:<br>
>> ><br>
>> > TRIM_API_URL = '<a href="http://api.tr.im/api" target="_blank">http://api.tr.im/api</a>'<br>
>> > response =<br>
>> > urllib2.urlopen('%s/trim_simple?url=%s&username=%s&password=%s'<br>
>> > % (TRIM_API_URL,<br>
>> > url_to_trim,<br>
>> > USERNAME,<br>
>> > PASSWORD))<br>
>> > url = response.read().strip()<br>
>> ><br>
>> > ...then not only is url valid but it's recorded in my <a href="http://tr.im" target="_blank">tr.im</a> account.<br>
>> > (Though response.code is still 200.)<br>
>> ><br>
>> > Either there must be something wrong in my authentication code, or in<br>
>> > <a href="http://tr.im" target="_blank">tr.im</a>'s. (I've written <a href="mailto:api@tr.im" target="_blank">api@tr.im</a> to see if it's a known problem and<br>
>> > haven't received a response.)<br>
>> ><br>
>> > Can anyone spot any problems?<br>
>> ><br>
>> > --<br>
>> > Daryl<br>
>> > _______________________________________________<br>
>> > Baypiggies mailing list<br>
>> > <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
>> > To change your subscription options or unsubscribe:<br>
>> > <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
>> ><br>
>><br>
>><br>
>><br>
>> --<br>
>> In this life we cannot do great things. We can only do small things<br>
>> with great love. -- Mother Teresa<br>
>> <a href="http://jjinux.blogspot.com/" target="_blank">http://jjinux.blogspot.com/</a><br>
>> _______________________________________________<br>
>> Baypiggies mailing list<br>
>> <a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
>> To change your subscription options or unsubscribe:<br>
>> <a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
><br>
><br>
_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org" target="_blank">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br>
</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br></blockquote></div><br><br clear="all"><br>-- <br>---<br>Failures are finger posts on the road to achievement. <br>
<br> -- C.S. Lewis<br>