简体   繁体   English

使用Pickle在Python中进行套接字编程

[英]Socket Programming in Python using Pickle

I have used the pickle function in python for a socket proogramming question. 我已将python中的pickle函数用于套接字编程问题。 But the ouput I receive at the server is printed as "< main .ProcessData instance at 0x7fbacba37f38>" instead of what is sent. 但是,我在服务器上收到的输出将打印为“ <0x7fbacba37f38的main .ProcessData实例”,而不是发送的内容。

The Server and Client code are as follows: 服务器和客户端代码如下:

SERVER 服务器

import socket, pickle

class ProcessData:
    print "Server is Listening....." 


print "Server is Listening....."
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr

data = conn.recv(4096)
data_variable = pickle.loads(data)
conn.close()
print data_variable
print 'Data received from client'

CLIENT 客户

import socket, pickle

class ProcessData:
    print 'ABCDEFGHIJK'



HOST = 'localhost'
PORT = 50007
# Create a socket connection.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

# Create an instance of ProcessData() to send to server.
variable = ProcessData()
# Pickle the object and send it to the server
data_string = pickle.dumps(variable)
s.send(data_string)

s.close()
print 'Data Sent to Server'

I Am getting the following output: 我得到以下输出:

Server is Listening..... Server is Listening..... Connected by ('127.0.0.1', 34726) < main .ProcessData instance at 0x7f8e2dfaaf80> Data received from client 服务器正在侦听.....服务器正在侦听.....由('127.0.0.1',34726)< .ProcessData实例位于0x7f8e2dfaaf80>连接从客户端接收到的数据

But I have to get ABCDEFGHIJ. 但是我必须得到ABCDEFGHIJ。 What should I do? 我该怎么办?

It is not clear why you have print statements in your class declarations, but putting your data in a print statement the class declaration is certainly not what you want. 目前尚不清楚为什么在class声明中有打印语句,但是将数据放在打印语句中,类声明肯定不是您想要的。

You are properly pickling, sending, and receiving an object, but your object doesn't do anything. 您正在正确地腌制,发送和接收对象,但是您的对象不执行任何操作。

What you will presumably want is some module that has your shared data type, and then the client and server can communicate with that type. 您大概想要的是某个具有共享数据类型的模块,然后客户端和服务器可以使用该类型进行通信。

I created processdata.py with the following: 我使用以下代码创建了processdata.py

class ProcessData:
    def __init__(self, data= 'ABCDEFGHIJK'):
        self.data = data
    def __str__(self): return self.data

And then modified your code as such: 然后像这样修改您的代码:

CLIENT 客户

import socket, pickle
from processdata import ProcessData

HOST = 'localhost'
PORT = 50007
# Create a socket connection.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

# Create an instance of ProcessData() to send to server.
variable = ProcessData()
# Pickle the object and send it to the server
data_string = pickle.dumps(variable)
s.send(data_string)

s.close()
print 'Data Sent to Server'

SERVER 服务器

import socket, pickle

print "Server is Listening....."
HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr

data = conn.recv(4096)
data_variable = pickle.loads(data)
conn.close()
print data_variable
print 'Data received from client'

And then I get 然后我得到

Server is Listening.....
Connected by ('127.0.0.1', 50941)
ABCDEFGHIJK
Data received from client

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM