我正在尝试在Android模拟器上运行python脚本。但是,我正在拒绝连接" (URLError)在Python中。我的服务器在同一台笔记本电脑(localhost)和同一端口上运行。服务器设计用于打印终端本身发送给它的各种请求。但是,我甚至无法与Web服务器建立连接(即,它不会打印出由python脚本发送给它的Web请求)。但我可以通过在Web浏览器本身的地址栏上写入URL来访问Web服务器上的其他文件,服务器会相应地在终端上打印请求。
完整的python脚本如下:
import android
import json
import time
import urllib
import urllib2
hello_msg = "Welcome to Coach Kelly's Timing App"
list_title = "Here is your list of athletes:"
quit_msg = "Quitting Coach Kelly's App."
web_server = 'http://127.0.0.1:8080'
get_names_cgi = '/cgi-bin/generate_names.py'
def send_to_server(url, post_data=None):
if post_data:
page = urllib2.urlopen(url, urllib.urlencode(post_data))
else:
page = urllib2.urlopen(url)
return(page.read().decode("utf8"))
app = android.Android()
def status_update(msg, how_long=2):
app.makeToast(msg)
time.sleep(how_long)
status_update(hello_msg)
athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi)))
app.dialogCreateAlert(list_title)
app.dialogSetSingleChoiceItems(athlete_names)
app.dialogSetPositiveButtonText('Select')
app.dialogSetNegativeButtonText('Quit')
app.dialogShow()
resp = app.dialogGetResponse().result
status_update(quit_msg)
以下是我的追溯输出:
Traceback(most recent call last):
File "/mnt/sdcard/sl4a/scipts/coachapp_1.py", line 37 in <module>
athlete_names = sorted(json.loads(send_to_server(web_server + get_names_cgi)))
File "/mnt/sdcard/sl4a/scipts/coachapp_1.py", line 20 in send_to_server
page = urllib2.urlopen(url)
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 124 in urlopen
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 383 in open
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 401 in _open
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 361 in _call_chain
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 1130 in http_open
File "/home/manuel/AptanaStudio3Workspace/python-for-android/python-build/output/usr/lib/python2.6/urllib2.py", line 1105 in do_open
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
我提到了有关此讨论的其他主题,但无法得到一个非常令人满意的答案。因此,在清除URLError
时会有任何帮助