我正在编写一个与 Google API 交互的 Python 应用程序,并需要通过 oauth2 进行用户身份验证。
我目前正在设置一个本地身份验证服务器,以接收从 Google 的 oauth 服务器返回的 oauth2 身份验证代码,基本上就像进行 oauth 舞蹈一样this http://code.google.com/apis/accounts/docs/OAuth2.html#installed.
它通常工作得很好,但我想我并不清楚它是如何与我的端口交互的,因为它会很高兴地分配我的本地身份验证服务器在端口 8080 上运行,即使其他应用程序(在我的测试中,SABNzbd++ )正在使用该端口。
我认为将端口分配给已使用的端口号会导致错误并重试。我做错了什么(或者,SABNzbd++ 做了什么,使它在我的应用程序中隐藏了它正在监听端口 8080 的事实?)
相关代码如下。
import socket
import BaseHTTPServer
from oauth2client.tools import ClientRedirectServer, ClientRedirectHandler
port_number = 0
host_name = 'localhost'
for port_number in range(8080,10000):
try:
httpd = ClientRedirectServer((host_name, port_number),
ClientRedirectHandler)
except socket.error, e:
print "socket error: " + str(e)
pass
else:
print "The server is running on: port " + str(port_number)
print "and host_name " + host_name
break
为了澄清,以下是我的预期成绩
socket error: [port already in use] (or something like that)
The server is running on: port 8081
and host_name localhost
然后转到 localhost:8080 解析为 SABnzbd+,而 localhost:8081 解析为我的身份验证服务器。
然而,我得到了:
the server is running on: port 8080
and host_name localhost
但转到 localhost:8080 解析为 SABNzbd+
提前致谢!