简体   繁体   中英

Socket Programming in Python using Pickle

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:

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

But I have to get 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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