【问题标题】:Python script that is not running in Windows server 2012未在 Windows Server 2012 中运行的 Python 脚本
【发布时间】:2017-08-16 17:27:21
【问题描述】:

我有一个脚本已经成功运行了很长时间。最近我的一台服务器升级到 Server 2012 并且脚本无法运行。它的目的是连接到一个端口并接收一些数据。这里的问题看起来像是我收到的一些奇怪的字符,我不知道它们来自哪里。任何帮助将不胜感激。

失败:

"C:\Python27\python.exe" "C:\gui_update_client.py" 主机名/IP 地址 10000 文件名服务器:主机名/IP 地址,端口:10000,主服务器: 文件名连接到端口 10000 上的 nn.nnn.nnn.nn GUI 更新请求 发送:文件名接收数据:€$Y{gï 接收数据:套接字错误: 没有收到数据。

成功:

"C:\Python27\python.exe" "C:\gui_update_client.py" 主机名/IP 地址 10000 文件名服务器:主机名/IP 地址,端口:10000,主服务器: 文件名连接到端口 10000 上的主机名/IP 地址 GUI 更新 请求发送:文件名接收数据:保持活动

Keep alive received 收到数据:GUI 更新完成

响应读取:GUI 更新完成

# Script Arguments
import sys
# Network Client
import socket
# Logging
import logging
# Other
import time
import os

TIMEOUT = 60
DELIM = '\n'
KEEP_ALIVE_MSG = 'Keep alive'

NO_UPDATES_FOUND = 'No GUI updates found'
UPDATE_COMPLETED = 'GUI update completed'

# Check required arguments
try:
    server = sys.argv[1]
    port   = int(sys.argv[2])
    master = sys.argv[3]
except:
    print 'Usage: {0} server port master'.format(sys.argv[0])
    sys.exit(1)

print 'Server: {0}, Port: {1}, Master: {2}'.format(server, port, master)

# Create a TCP/IP socket
sock = socket.create_connection((server, port))
sock.settimeout(TIMEOUT)
print 'Connected to {0} on port {1}'.format(server, str(port))

# Send request and get response
response = ''
try:
    # Send GUI update request
    sock.sendall(master + DELIM)
    print 'GUI update request sent: {0}'.format(master)

    # Read the response
    data  = ''
    while response == '':
        # Read the response
        split = ''
        while split != DELIM:
            # Buffer the data in small chunks
            datum = sock.recv(1024)
            print 'Data received: {0}'.format(datum)
            if datum == '':
                raise socket.error('No data received.')
            data += datum
            (response, split, remainder) = data.partition(DELIM)

        # Check for keep alive response
        while response == KEEP_ALIVE_MSG:
            print 'Keep alive received'
            data = remainder
            (response, split, remainder) = data.partition(DELIM)
            if split != DELIM:
                response = ''

    # Response received
    print 'Response read: {0}'.format(response)
except socket.timeout as err:
    print 'Timeout error: {0}'.format(str(err))
except socket.error as err:
    print 'Socket error: {0}'.format(str(err))
finally:
    # Clean up the connection
    sock.close()
    logging.info('Connection to {0} on port {1} closed.'\
                 .format(server, str(port)))

    if response == UPDATE_COMPLETED or response == NO_UPDATES_FOUND:
        os.environ['ERRORLEVEL'] = "0"
        exit()
    else:
        os.environ['ERRORLEVEL'] = "1"
        exit(1)

提前谢谢你。

【问题讨论】:

  • 你能发布 gui_update_client.py 吗?
  • @Wyrmwood 我根据您的要求进行了编辑。谢谢

标签: python python-2.7 windows-server-2012


【解决方案1】:

感谢您的观看。我发现了这里的问题。剧本很好。这里的问题是 Windows 侦听端口位于不同的网络上。当服务器升级到 2012 时,它默认使用不同的网卡来运行服务。这就是为什么回复带有奇怪的字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多