TypeError: descriptor 'bind' requires a '_socket.socket' object but received a 'tuple'

502 Views Asked by At

I am trying to make a simple socketing server and client by Python.But when I run the Server code,It shows

Traceback (most recent call last):
  File "G:/Python/pyProject/TestOfConnection/socket_sever_pickle.py", line 33, in <module>
    Server_PIC(Server_IP, Server_Port)
  File "G:/Python/pyProject/TestOfConnection/socket_sever_pickle.py", line 8, in Server_PIC
    socket.bind((ip,port))
TypeError: descriptor 'bind' requires a '_socket.socket' object but received a 'tuple'
Here it is:

from socket import *
from io import BytesIO
import pickle


def Server_PIC(ip, port):
    socket_obj = socket(AF_INET, SOCK_STREAM)
    socket.bind((ip,port))
    socket_obj.listen(10)
    file_no = 1
    while True:
        connection, address = socket_obj.accept()
        print("server connect by:", address)
        recieved_message = b''
        recieved_message_fragment = connection.recv(1024)
        while recieved_message_fragment:
            recieved_message += recieved_message_fragment
            recieved_message_fragment = connection.recv(1024)
            try:
                obj = pickle.loads(recieved_message)
                print(obj)
            except EOFError:
                file_name = 'recv_image_' + str(file_no) + '.bmp'
                recv_image = open(file_name, 'wb')
                recv_image.write(recieved_message)
                recv_image.close()
            connection.close()


if __name__ == '__main__':
    Server_IP = '0.0.0.0'
    Server_Port = 6666
    Server_PIC(Server_IP, Server_Port)

I also try to Google this question and modify the code,but it will cause a series of problems. So could any of you help me figure out what is going on wiht my code. Thanks in advance!

1

There are 1 best solutions below

0
xiaoqu On

When I modify the code,the error disappeared.

# from socket import *
import socket
from io import BytesIO
import pickle


def Server_PIC(ip, port):
    # socket_obj = socket(AF_INET, SOCK_STREAM)
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((ip,port))
    s.listen(10)
    file_no = 1
    while True:
        connection, address = s.accept()
        print("server connect by:", address)
        recieved_message = b''
        recieved_message_fragment = connection.recv(1024)
        while recieved_message_fragment:
            recieved_message += recieved_message_fragment
            recieved_message_fragment = connection.recv(1024)
            try:
                obj = pickle.loads(recieved_message)
                print(obj)
            except EOFError:
                file_name = 'recv_image_' + str(file_no) + '.bmp'
                recv_image = open(file_name, 'wb')
                recv_image.write(recieved_message)
                recv_image.close()
            connection.close()


if __name__ == '__main__':
    Server_IP = '0.0.0.0'
    Server_Port = 6666
    Server_PIC(Server_IP, Server_Port)