使用Pickle在Python中进行套接字编程 [英] Socket Programming in Python using Pickle

查看:63
本文介绍了使用Pickle在Python中进行套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python中使用了pickle函数来解决套接字编程问题.但是,我在服务器上收到的输出被打印为< 主要.ProcessData实例位于0x7fbacba37f38>",而不是发送的内容.

I have used the pickle function in python for a socket proogramming question. But the ouput I receive at the server is printed as "<main.ProcessData instance at 0x7fbacba37f38>" instead of what is sent.

服务器和客户端代码如下:

The Server and Client code are as follows:

服务器

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)< 主要.ProcessData实例位于0x7f8e2dfaaf80>从客户那里收到的数据

Server is Listening..... Server is Listening..... Connected by ('127.0.0.1', 34726) <main.ProcessData instance at 0x7f8e2dfaaf80> Data received from client

但是我必须得到ABCDEFGHIJ.我该怎么办?

But I have to get ABCDEFGHIJ. What should I do?

推荐答案

目前尚不清楚为什么在 class 声明中有打印语句,但是将数据放在print语句中是类声明当然不是您想要的.

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.

您可以正确地腌制,发送和接收对象,但是您的对象没有任何作用.

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.

我使用以下内容创建了 processdata.py :

I created processdata.py with the following:

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

然后像这样修改您的代码:

And then modified your code as such:

客户

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

这篇关于使用Pickle在Python中进行套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆