O'Reilly Forums: Page 276 Program Error - O'Reilly Forums
Page 276 Program Error
#1
Posted 25 July 2011 - 10:50 PM
I am trying to run the program on page 276 on the android emulator. I am getting an error saying "<ulropen error [Error 111] Connection Refused>". I googled the error. As per the explanation on this link it happens if server is not running.
I am attaching the screenshot of the emulator and the command prompt. Can someone help out??
Dharmit
#2
Posted 26 July 2011 - 12:19 AM
#3
Posted 26 July 2011 - 01:35 AM
paulbarry, on 26 July 2011 - 12:19 AM, said:
I changed the ip address to that of the computer I am coding upon. On the emulator's browser, the ip address i.e. 192.168.1.8:8080 opens up the Coach Kelly homepage.
From the link that you gave me, I changed the
from urllib2 import urlopento
from urllib import urlopen
Now I get a new kind of error which I am attaching with the post.
#4
Posted 26 July 2011 - 03:41 AM
#5
Posted 26 July 2011 - 10:11 PM
paulbarry, on 26 July 2011 - 03:41 AM, said:
That was a dumb typo on my part. I corrected it. Later I tried to execute the script. But now I have an error saying, "No JSON object decoded". I googled it but couldn't find something that would help me. On stackoverflow one person with similar problem had forgot to do
urlopen.read()which I guess is not an issue with the code in the book as we are doing
page.read().decode("utf-8") I have attached the screenshot in such a way that you can also take a look at the entire code.
#6
Posted 27 July 2011 - 07:55 AM
#7
Posted 28 July 2011 - 01:06 AM
paulbarry, on 27 July 2011 - 07:55 AM, said:
Paul, I fail to understand what I should do in order to proceed further. I am stuck on this page since quite a few days. All I could think of after reading your reply was
def send_to_server(url, post_data = None):
if post_data:
page = urlopen(url, urlencode(post_data))
print ("If")
else:
page = urlopen(url)
print ("Else")
return (page.read().decode("utf-8"))I have added print statements here. As expected it prints "Else" and then the same error message. In the generate_names.py I couldn't understand where to add the print statement. I added a print statement and now my generate_names.py file looks like
import json
import athletemodel
import yate
print "Hello"
names = athletemodel.get_names_from_store()
print(yate.start_response('application/json'))
print(json.dumps(sorted(names)))
But Hello doesn't get printed on the screen. I am not sure if I did the right thing but somehow I feel it's not gonna be right.
#8
Posted 28 July 2011 - 04:53 AM
import sys
to the top of the file, then add:
print('The data was: ' + names, file=sys.stderr)
before the 2 print calls at the bottom of the code. Remove your call to print 'Hello' (which is not valid Python 3 code anyway).
What this will do is spit up a message on the web server's console screen (not the web browser window) and display the result of the server attempting to create the data to send to the phone. If there's no data displayed in the console window, then that's your problem - the data files are missing. If the data is there, then there's some other problem. Note that the code on the phone should be *exactly* as displayed in the book (no matter what other Google searches may tell you).
Try that and see what happens.
--Paul.
#9
Posted 28 July 2011 - 10:51 PM
paulbarry, on 28 July 2011 - 04:53 AM, said:
import sys
to the top of the file, then add:
print('The data was: ' + names, file=sys.stderr)
before the 2 print calls at the bottom of the code. Remove your call to print 'Hello' (which is not valid Python 3 code anyway).
What this will do is spit up a message on the web server's console screen (not the web browser window) and display the result of the server attempting to create the data to send to the phone. If there's no data displayed in the console window, then that's your problem - the data files are missing. If the data is there, then there's some other problem. Note that the code on the phone should be *exactly* as displayed in the book (no matter what other Google searches may tell you).
Try that and see what happens.
--Paul.
If I keep the code exactly as it is in the book, it would be
from urllib2 import urlopen
It gives me 404 Error page. And if I keep
from urllib import urlopen
it gives me Json object could not be decoded error.
The error I get on the console is attached with the post. It's again a 404 error. Opening 192.168.1.9:8080 on my desktop on which server is running lists C: of the machine. Why does it do so? The command that I run is
C:\Python32\python.exe E:\hfpython\my_hfp_code\chapter8\webapp\simple_httpd.py
#10
Posted 29 July 2011 - 12:10 AM
C:\Python32\python.exe E:\hfpython\my_hfp_code\chapter8\webapp\simple_httpd.py
which runs the simple_httpd.py script in "E:\hfpython\my_hfp_code\chapter8\webapp\", right? In order for the web server to find the CGI script and its data, it needs to run relative to the folder that contains the simple_httpd.py code. So, at your command-prompt, do this:
cd E:\hfpython\my_hfp_code\chapter8\webapp\
then issue this command:
C:\Python32\python.exe simple_httpd.py
Now when the web server runs, it'll find the data as well as the CGI script in the same folder as the simple_httpd.py program.
Try that and let's see what happens...
--Paul.
#11
Posted 29 July 2011 - 03:37 AM
paulbarry, on 29 July 2011 - 12:10 AM, said:
C:\Python32\python.exe E:\hfpython\my_hfp_code\chapter8\webapp\simple_httpd.py
which runs the simple_httpd.py script in "E:\hfpython\my_hfp_code\chapter8\webapp\", right? In order for the web server to find the CGI script and its data, it needs to run relative to the folder that contains the simple_httpd.py code. So, at your command-prompt, do this:
cd E:\hfpython\my_hfp_code\chapter8\webapp\
then issue this command:
C:\Python32\python.exe simple_httpd.py
Now when the web server runs, it'll find the data as well as the CGI script in the same folder as the simple_httpd.py program.
Try that and let's see what happens...
--Paul.
Phew.. That finally worked. I had actually forgot what the desired output was. Past few days were like shuttling between "404" and "json object not decoded".
Just one point that I observed. Using urllib or urllib2 doesn't make difference in this application. So why do two separate modules exist?
Thanks
Dharmit
#13
Posted 03 August 2011 - 09:52 PM
Starting a new topic for the same program didn't seem worth so I am continuing here.
After tapping on the user ans clicking Select button, I get No Json object decoded error once again. I think that it's not able to sort the list of times. But I don't understand why.
I am attaching screenshot of prompt, emulator and generate.py files with the post.
Dharmit
#14
Posted 04 August 2011 - 05:39 AM
#15
Posted 31 January 2012 - 08:03 PM
#16
Posted 31 January 2012 - 10:01 PM
chrisvetek, on 31 January 2012 - 08:03 PM, said:
update, 1 am.
I GOT IT. Sorry to bother you, I feel like dancing now.


















