Python - 简单的 TCP/IP S/C : Not all strings converted error for a print statement

标签 python tcp client server aptana

好的,所以我设置了一个简单的套接字 TCP/IP 服务器和客户端,现在我在 Aptana Atudio 3 中的 PyDev 中遇到了这个错误。服务器运行良好并等待连接,但是我的客户端运行时给出我的错误

print '正在与位于 %s 的服务器建立连接 ' % server_address

TypeError:在字符串格式化期间并非所有参数都已转换

为了尝试自己解决问题,我在堆栈之前访问了我的常规资源,并将我的代码与 PYMOTW 上的文章进行了比较并调整以匹配他们的,看看它是否改变了我的错误。我似乎看不出是什么导致了错误。我将我的解释器设置为与我的电脑运行时相同的 python 语言...这是我的代码,我已经搜索了其他用户提出的具有相同问题的问题,但似乎总有一个修复程序不适用于他们的我..

import socket
import sys


sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # @UndefinedVariable

server_address = ('localhost', 0)
print 'establishing connection to server at %s' % server_address
sock.connect(input(server_address))  # @UndefinedVariable

try:
    message = 'message to server'
    print 'sending %s' % message
    sock.sendall(message) 

    amount_recieved = 0 
    amount_expected = len(message)  # @UndefinedVariable

    while amount_recieved < amount_expected:
        data = sock.recv(16)
        amount_recieved += len(data)  # @UndefinedVariable
        print 'recieved %s' %(data)
finally:
    print 'closing down'
    sock.close

()

最佳答案

server_address 是一个元组。试试这个:

print 'establishing connection to server at %s ' % str(server_address)

或者这个:

print 'establishing connection to server at %s:%d ' % server_address

关于Python - 简单的 TCP/IP S/C : Not all strings converted error for a print statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29829171/

相关文章:

python - 当您有多个具有相同名称的类时,在 python 中抓取 html

python - pandas 中 header 和 Skiprow 的区别不清楚

connection - 从客户端检测 SignalR 中丢失的连接

python - 距行中第一个非空元素的距离的 numpy 矩阵

python - 选择字段填充错误

C# 套接字 TCP 客户端/服务器

swift - 我想在 swift 中使用 outputStream.write() 通过输出流发送 Int

c# - 知道网络流何时关闭

java - 无法从java客户端发送参数

android - 从 Android(作为客户端)发送 TCP 数据 - 没有发送数据?