Python TypeError: an integer is required while working with Sockets on the client side
can some one help please, i am running my code connecting the client to the server that connects to a database and when i am sending the info from the client to the server with the ID as an int, an error would appear saying: TypeError: must be string or buffer, not int. And when changing the ID into a string at the server side, an error would appear saying: TypeError: an integer is required
Please help me because i got confused...
import os import sys import socket import sqlite3 as lite
global s 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."
class main: # create a socket object c = client() Register = "Register" Login = "Login"
# 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'))
You cannot call s.send() like this:
s.send(ID, Name) s.send(Email, Password)
When you call that method with two arguments, the second argument is supposed to be an integer. Specifically, it is supposed to be the bitwise OR of zero or more "flag values"; a list can be found here (scroll down a bit). Zero means no flags and is probably what you want for most simple cases. The argument defaults to zero if you do not pass it at all. For this case, I imagine you want to write four separate calls to send(), like this:
s.send(ID) s.send(Name) s.send(Email) s.send(Password)
Please also bear in mind that TCP is a "stream-oriented" protocol (SOCK_STREAM), meaning the above is exactly the same as this:
everything = ''.join([ID, Name, Email, Password]) # Equivalent to but faster than: everything = ID + Name + Email + Password s.send(everything)