473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sending pickled dictionary over a socket

I saw this thread:
http://bytes.com/topic/python/answers/553952-sending-dictionary-via-network

which works nicely for tiny dictionaries, but my sample pickled dictionary is about 500,000 characters long, and this method doesn't work. I tried splitting the dictionary on \n and then joining it, but the sockets hang after a coupla thousand lines.

Surely, passing a dictionary over a socket must be a fairly common request? how is it done?
Aug 23 '10 #1

✓ answered by Ivan Dewolf

hokay, solved it. Brak it into chunks and send 'em.

The server launches the client .py

server snippet:

Expand|Select|Wrap|Line Numbers
  1. mysocket = socket(AF_INET,SOCK_DGRAM)
  2. mysocket.bind(addr)
  3. #launch client.py
  4. clientthread().start()
  5. pkl_str = ""
  6. Dlen = mysocket.recv(buf)
  7. for i in range(0,int(int(Dlen)/buf)+1):
  8.     data = mysocket.recv(buf)
  9.     pkl_str +=data
  10. dict = pickle.loads(pkl_str)
  11.  

client snippet:
Expand|Select|Wrap|Line Numbers
  1. ### Create socket
  2. mysocket = socket(AF_INET,SOCK_DGRAM)
  3.  
  4. ### Send pickle length
  5. mysocket.sendto(str(len( pkl_dict)),addr)
  6. i = 0
  7. j = buf
  8. while(i<len(pkl_dict)):
  9.     if j>(len(pkl_dict)-1):
  10.         j=(len(pkl_dict))
  11.     ###send pickle chunks
  12.     mysocket.sendto(pkl_dict[i:j],addr)
  13.     i += buf
  14.     j += buf
  15.  

5 9940
also, security is not an issue, I am aware of the security issues.

Here's some code fragments:
Expand|Select|Wrap|Line Numbers
  1. pkl_dict = cPickle.dumps(heir_dict)
  2. pkl_split = pkl_dict.split("\n")
  3. print len(pkl_split)
  4. i = 0
  5. for line in pkl_split:
  6.     i += 1
  7.     print str(i)+" "+line
  8.     mysocket.send(line)
  9.  
my sample pickle returns len(pkl_split) = 45499
and hangs when i = 2707
Aug 23 '10 #2
hokay, solved it. Brak it into chunks and send 'em.

The server launches the client .py

server snippet:

Expand|Select|Wrap|Line Numbers
  1. mysocket = socket(AF_INET,SOCK_DGRAM)
  2. mysocket.bind(addr)
  3. #launch client.py
  4. clientthread().start()
  5. pkl_str = ""
  6. Dlen = mysocket.recv(buf)
  7. for i in range(0,int(int(Dlen)/buf)+1):
  8.     data = mysocket.recv(buf)
  9.     pkl_str +=data
  10. dict = pickle.loads(pkl_str)
  11.  

client snippet:
Expand|Select|Wrap|Line Numbers
  1. ### Create socket
  2. mysocket = socket(AF_INET,SOCK_DGRAM)
  3.  
  4. ### Send pickle length
  5. mysocket.sendto(str(len( pkl_dict)),addr)
  6. i = 0
  7. j = buf
  8. while(i<len(pkl_dict)):
  9.     if j>(len(pkl_dict)-1):
  10.         j=(len(pkl_dict))
  11.     ###send pickle chunks
  12.     mysocket.sendto(pkl_dict[i:j],addr)
  13.     i += buf
  14.     j += buf
  15.  
Aug 24 '10 #3
Oralloy
988 Expert 512MB
Ivan,

Good on you!

Is there a reason you're using UDP, and not IP?

Cheers!
Aug 24 '10 #4
um.... out of ignorance?
to be honest, I'm a bit fuzzy as to how to choose between the two.
Aug 24 '10 #5
Oralloy
988 Expert 512MB
Ivan,

I know that unknowing feeling.

UDP Datagrams are non-guaranteed delivery. They're great for sending very short messages when you don't know if you'll get a response.

IP Datastreams are called "reliable", as they have retransmit and sequencing built into their delivery.

In general I suggest using IP (TCP/IP) for "conversations" between computers, like FTP or Telnet.

Datagrams are good for quick messages where losing information is Ok. One common use of datagrams is protocols like DHCP. The client system (e.g. your desktop) sends a broadcast datagram asking for a valid address or identifier token. One or more servers may reply, even though the client only needs one.

Does that help?
Aug 24 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Martin | last post by:
I am a PHP newbie (just got my "Hello World" page working this morning). I'm doing some R&D work to see if PHP is viable for a situation I have. To accomplish what I want to do, I have to have the...
3
by: Ajay | last post by:
hi! on my client side, i have a socket that sends and then receives the code is s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((PDA_ADDRESS, PDA_PORT)) s.send("persona=")...
2
by: John Lemire | last post by:
Hi, I have a connection which after an initial handshake has one way traffic. I am sending bursty data. When I write a data buffer (variable size) the last portion of it is not sent to the server...
6
by: Bruce Vander Werf | last post by:
I am using the asynchronous send/receive methods of the Socket class. When the remote end closes the socket, the callback for receive is called and EndReceive returns 0. Socket.Connected still...
2
by: banduraj | last post by:
I am working on starting and managing TCP connections manually. I build the IP headers and TCP packets manually and send them on my own. The problems I run into seems to be related to the Sockets....
10
by: Martin Bürkle | last post by:
Hi NG, I have writen a programm using TCP sockets. After i get the connection to another socket I cut the Ethernet cable. Then I send a message. The program doesnt raise any exception. Can...
5
by: MasterChief | last post by:
Hy! I am trying to write a programm which can send "user defined" messages over the network to a client and receive the messages. The Message is a class I have written and I am using recv() and...
13
by: trymore | last post by:
Hi all, can anybody help me out there? I am developing a client sockect to send transactions form a DB to a server socket. the way i want is to connect, send, recieve , disconnect, connect, send,...
4
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I need to send a file over a socket to a https address. Do I need to do anything differently than specify the address with the https, and port 443? For instance, string m_server=...
1
by: sachin2 | last post by:
I am using 3 types of dictionaries. 1) Dictionary<string, string > d = new Dictionary<string, string>(); 2) Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); 3)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.