开发者

Easiest Way to Transfer Data Over the Internet, Python

I have two computers开发者_JAVA技巧, both are connected to the internet. I'd like transfer some basic data between them (strings, ints, floats). I'm new to networking so I'm looking for the most simple way to do this. What modules would I be looking at to do this?

Both systems would be running Windows 7.


As long as its not asynchronous (doing sending and receiving at once), you can use the socket interface.

If you like abstractions (or need asynchronous support), there is always Twisted.

Here is an example with the socket interface (which will become harder to use as your program grows larger, so, I would suggest either Twisted or asyncore)

import socket

def mysend(sock, msg):
    totalsent = 0
    while totalsent < MSGLEN:
        sent = sock.send(msg[totalsent:])
        if sent == 0:
            raise RuntimeError("socket connection broken")
        totalsent = totalsent + sent

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect(("where ever you have your other computer", "port number"))

i = 2
mysend(s, str(i))

The python documentation is excellent, I picked up the mysend() function from there.

If you are doing computation related work, check out XML-RPC, which python has all nicely packaged up for you.

Remember, sockets are just like files, so they're not really much different to write code for, so, as long as you can do basic file io, and understand events, socket programming isn't hard, at all (as long as you don't get too complicated like multiplexing VoIP streams...)


If you have completely no idea of what socket is, it might be a bit difficult to use Twisted. And as you need to identify the type of the data being transferred, things will be harder.

So perhaps the python version of ICE, the Internet Communication Engine will be more suitable for because it hides a lot of dirty details of network programming. Have a look of the hello world to see if it does your work.


Look here: If you ,as I think you are, trying to use sockets this is what you are looking for:https://docs.python.org/2/howto/sockets.html

I hope this will help as it worked well for me. or add this class for constant connection:

class mysocket:
    '''demonstration class only
      - coded for clarity, not efficiency
    '''

    def __init__(self, sock=None):
        if sock is None:
            self.sock = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock

    def connect(self, host, port):
        self.sock.connect((host, port))

    def mysend(self, msg):
        totalsent = 0
        while totalsent < MSGLEN:
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent

    def myreceive(self):
        chunks = []
        bytes_recd = 0
        while bytes_recd < MSGLEN:
            chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
            if chunk == '':
                raise RuntimeError("socket connection broken")
            chunks.append(chunk)
            bytes_recd = bytes_recd + len(chunk)
        return ''.join(chunks)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜