可以帮助一些人,我正在运行我的代码将客户端连接到连接到数据库的服务器,当我从客户端向服务器发送ID为int的服务器时,会出现错误:TypeError:必须是字符串或缓冲区,而不是int 当在服务器端将ID更改为字符串时,会出现错误:TypeError:需要整数
请帮助我,因为我感到困惑......
#!的/ usr / bin中/ Python的
导入操作系统 导入系统 导入套接字 将sqlite3导入为lite
全球的 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)班级客户:
#functions on the client side only send the data to the server side (their only job)
def login(ID, password):
try:
s.send(self, ID)
s.send(password)
except IOError:
print "Error! Cannot execute statement."
def signup(self, ID, Name, Email, Password):
try:
s.send(ID, Name)
s.send(Email, Password)
except IOError:
print "Error! Cannot execute statement."
def addContact(self, ID, name, email):
try:
s.send(ID, name)
s.send(email)
except IOError:
print "Error! Cannot execute statement."
班级主要: #创建一个套接字对象 c = client() 注册=“注册” 登录=“登录”
# get local machine name
host = socket.gethostname()
port = 9999
# connection to hostname on the port.
s.connect((host, port))
Message = input("Login if you are a user. If you are new, register here so you could play checkers! \t")
if Message == Login:
ID = input("ID \t")
password = input("Password \t")
c.login(ID, password)
elif Message == Register:
ID = input("ID \t")
Name = input("Name \t")
Email = input("Email \t")
Password = input("Password \t")
c.signup(ID, Name, Email, Password)
elif Message == add:
ID = input("ID \t")
Name = input("Name \t")
Email = input("Email \t")
c.addContact(ID, name, email)
else:
exit()
# Receive no more than 1024 bytes
data = s.recv(1024)
s.close()
print("The time got from the server is %s" % data.decode('ascii'))
答案 0 :(得分:0)
您无法像这样致电s.send()
:
s.send(ID, Name)
s.send(Email, Password)
当您使用两个参数调用该方法时,第二个参数is supposed to be an integer。具体来说,它应该是零个或多个"标志值的按位OR&#34 ;;可以找到一个列表here(向下滚动一下)。零表示没有标志,可能是大多数简单情况下你想要的。如果您根本不传递参数,则参数默认为零。对于这种情况,我想你想写四个单独的send()
调用,如下所示:
s.send(ID)
s.send(Name)
s.send(Email)
s.send(Password)
请注意,TCP是一个面向流的"协议(SOCK_STREAM
),意思是上面与此完全相同:
everything = ''.join([ID, Name, Email, Password])
# Equivalent to but faster than: everything = ID + Name + Email + Password
s.send(everything)