2016-07-13 12 views
1

我有一個工作的python/tk程序根據用戶選擇運行cgi腳本。我正在努力將其縮減爲一個只關注一個特定cgi腳本的小腳本。它似乎正確地獲得會話ID,但是當我啓動瀏覽器時,我一直在「拒絕訪問」。由於其他程序的作品,我不期望從網站上的任何問題。任何幫助將不勝感激。接收到有效會話ID後拒絕訪問

UPDATE: 如果我使用一個調試器,設置斷點上線打印網址打印到控制檯上的URL,如下圖所示,沒有工作。我現在知道會話id令牌是好的。

另外,如果我步入的web瀏覽器功能,之後的腳本也工作那麼步過

這是我的代碼。

import json 
import tornado.web 
import tornado.websocket 
from tornado import gen 
import tornado.ioloop 
import webbrowser 
from struct import * 

request_id = 71 
ip_address = "10.22.4.14" 


# ************************************************ 
# Procedure to open websocket and get session id 
# *********************************************** 
@gen.coroutine 
def open_ws(ip, username, password): 
    global client 
    global request_id 
    global session_id 

    ws_url = "ws://" + ip + ":7011/" 

    try: 
     client = yield tornado.websocket.websocket_connect(ws_url, None, None, 5, None, None) 
     # print("websocket %s open" % ws_url) 
    except error: 
     exit() 

    # Send Mercury login request 
    JSON = '{"requests":[{"request_id": %s, "login":{"username": "%s","password": "%s"}}]}' % (str(request_id), username, password) 

    client.write_message(JSON) 
    results = yield client.read_message() 
    # print("msg is %s" % results) 

    # Parse the response of login request to get the error code 
    parsed_json = json.loads(results) 
    err_code = parsed_json['responses'][0]['request_response']['result']['err_code'] 

    if 0 == err_code: 
     # Parse the response of get_command_result to get the session id 
     session_id = parsed_json['responses'][0]['request_response']['login']['session_id'] 
     # print("login succeeded - session id: %s" % session_id) 
    else: 
     print("login failed") 
    # error_exit(err_code) 


def get_token(): 
tornado.ioloop.IOLoop.instance().run_sync(lambda: open_ws(ip_address, 'admin', 'admin')) 
    return session_id 

session_id = get_token() 
print "Token is " + session_id 

url = "http://" + ip_address + "/scripts/dostuff.cgi?session=" + session_id 
print url # add breakpoint here 

# launch browser 
webbrowser.open(url) 

控制檯輸出:

Token is 7zNSZX9liaUDFFN0ijn-LWQ8 
http://10.222.4.14/scripts/dostuff.cgi?session=7zNSZX9liaUDFFN0ijn-LWQ8 
+0

Web服務器是否期望一個cookie以及一個會話ID?或者它是否期望會話ID作爲cookie而不是查詢字符串來傳遞? –

+0

不。這是python/tk應用程序中使用的完全格式。 –

回答

0

解決。該腳本已結束,因此在瀏覽器有機會響應請求之前關閉套接字