简体   繁体   English

可以使用 Python 中的 Pickle 和套接字模块将数据对象从客户端发送到服务器吗?

[英]Can send data objects from client to server using the Pickle and socket modules in Python?

I want to my client program to send objects to server.我希望我的客户端程序将对象发送到服务器。 By searching the internet, in all the examples, I have seen data objects are sent by the server to the client, but I want to do the opposite.通过搜索互联网,在所有示例中,我都看到数据对象是由服务器发送到客户端的,但我想做相反的事情。 Below I am sharing both the client codes and the server code.下面我分享客户端代码和服务器代码。 I have been able to modify the socket client code, but I am having difficulty in modifying the server code.我已经能够修改套接字客户端代码,但我在修改服务器代码时遇到了困难。 As, I am new to python programming and I am on a deadline, a help is greatly appreciated.因为,我是 python 编程的新手,而且我正处于最后期限,非常感谢您的帮助。

Here are the codes:以下是代码:

Server code::服务器代码::

import socket



def server_program():

    # get the hostname
    host = socket.gethostname()
    port = 5000  # initiate port no above 1024
    server_socket = socket.socket()  # get instance
    # look closely. The bind() function takes tuple as argument
    server_socket.bind((host, port))  # bind host address and port together

    # configure how many client the server can listen simultaneously
    server_socket.listen(2)
    conn, address = server_socket.accept()  # accept new connection
    print("Connection from: " + str(address))
    while True:
        # receive data stream. it won't accept data packet greater than 1024 bytes
        data = conn.recv(1024).decode()
        if not data:
            # if data is not received break
            break
        print("from connected user: " + str(data))
        data = input(' -> ')
        conn.send(data.encode())  # send data to the client

    conn.close()  # close the connection


if __name__ == '__main__':
    server_program()

Here is the client code modified with pickle::这是用pickle修改的客户端代码::

import socket
import pickle


def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5000  # socket server port number

    client_socket = socket.socket()  # instantiate
    client_socket.connect((host, port))  # connect to the server

    message = input(" -> ")  # take input

    while message.lower().strip() != 'bye':
        client_socket.send(message.encode())  # send message
        #data = client_socket.recv(1024).decode()  # receive response

        #print('Received from server: ' + data)  # show in terminal

        wastebin_attr = {1:"Wastebin_serial_number", 2:"Wastebin_type", 3:"Status" , 4:"Action", 5:"Zone", 6:"Latitude", 7:"Longitude"}
        mssg = pickle.dumps(wastebin_attr)
        print("from connected user: " + mssg)
        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection


if __name__ == '__main__':
    client_program()

To send data from the client to the server using the pickle module:使用 pickle 模块将数据从客户端发送到服务器:

data = pickle.dumps(data)
client_socket.send(data)

To receive the data on the servers end use:要在服务器端接收数据,请使用:

data = conn.recv(1024)
data = pickle.loads(data)

You could also use this to send from server to client by switching the client_socket and conn parts您还可以通过切换 client_socket 和 conn 部分来使用它从服务器发送到客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 将数据从客户端套接字发送到服务器套接字? - How to send data from client socket to server socket using Python? python 套接字将数据从客户端发送到服务器 - python socket send data from client to server 如何使用泡菜从服务器向客户端VIA套接字发送公钥 - How to send public key from server to client VIA socket using pickle 无法使用python从服务器套接字发送到客户端套接字 - Cant send from server socket to client socket using python 如何使用套接字编程将字典数据从客户端发送到 python 服务器以与 df2 进行比较? - How can I send dictionary data from client side to python server for comparison with df2 using socket programming? 将数据从Java Socket Server发送到Python客户端 - Send data from Java Socket Server to Python client 从 Android 客户端连续发送数据到 Python 服务器套接字 - Continuously Send Data from Android Client to Python Server Socket 如何使用 socket 使用 pickle 发送多个对象? - How do you send multiple objects using pickle using socket? Django 尝试不断从服务器向客户端发送数据时,通道无法“socket.send” - Django Channels can't "socket.send" when trying to constantly send data to client from server 使用python将带有类别的数据从客户端发送到服务器 - send data with categories from client to a server using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM