使用 Pickle 在 Python 中进行套接字编程
Socket Programming in Python using Pickle
我在 python 中使用了 pickle 函数来解决套接字编程问题。但是我在服务器上收到的输出打印为“<main.ProcessData instance at 0x7fbacba37f38>”而不是发送的内容。
Server和Client代码如下:
服务器
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'
客户
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'
我得到以下输出:
服务器正在侦听.....
服务器正在监听......
通过 ('127.0.0.1', 34726) 连接
<main.ProcessData 实例位于 0x7f8e2dfaaf80>
从客户端收到的数据
但我必须得到 ABCDEFGHIJ。
我该怎么办?
不清楚为什么在 class
声明中有打印语句,但是将数据放在打印语句中 class 声明肯定不是您想要的。
您正在正确地 pickle、发送和接收一个对象,但您的对象没有做任何事情。
您可能想要的是一些具有您共享数据类型的模块,然后客户端和服务器可以与该类型进行通信。
我使用以下内容创建了 processdata.py
:
class ProcessData:
def __init__(self, data= 'ABCDEFGHIJK'):
self.data = data
def __str__(self): return self.data
然后修改你的代码:
客户端
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'
服务器
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'
然后我得到
Server is Listening.....
Connected by ('127.0.0.1', 50941)
ABCDEFGHIJK
Data received from client
我在 python 中使用了 pickle 函数来解决套接字编程问题。但是我在服务器上收到的输出打印为“<main.ProcessData instance at 0x7fbacba37f38>”而不是发送的内容。
Server和Client代码如下:
服务器
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'
客户
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'
我得到以下输出:
服务器正在侦听..... 服务器正在监听...... 通过 ('127.0.0.1', 34726) 连接 <main.ProcessData 实例位于 0x7f8e2dfaaf80> 从客户端收到的数据
但我必须得到 ABCDEFGHIJ。 我该怎么办?
不清楚为什么在 class
声明中有打印语句,但是将数据放在打印语句中 class 声明肯定不是您想要的。
您正在正确地 pickle、发送和接收一个对象,但您的对象没有做任何事情。
您可能想要的是一些具有您共享数据类型的模块,然后客户端和服务器可以与该类型进行通信。
我使用以下内容创建了 processdata.py
:
class ProcessData:
def __init__(self, data= 'ABCDEFGHIJK'):
self.data = data
def __str__(self): return self.data
然后修改你的代码:
客户端
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'
服务器
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'
然后我得到
Server is Listening.....
Connected by ('127.0.0.1', 50941)
ABCDEFGHIJK
Data received from client