通过互联网传输数据的最简单方法,Python

发布于 2024-11-06 19:30:08 字数 118 浏览 5 评论 0原文

我有两台电脑,都连接到互联网。我想在它们之间传输一些基本数据(字符串、整数、浮点数)。我是网络新手,所以我正在寻找最简单的方法来做到这一点。我需要哪些模块来做到这一点?

两个系统都运行 Windows 7。

I have two computers, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分分钟 2024-11-13 19:30:08

只要它不是异步的(同时发送和接收),您就可以使用套接字接口。

如果您喜欢抽象(或需要异步支持),总有 Twisted。

这是一个套接字接口的示例(随着程序变大,它会变得更难使用,所以,我建议使用 Twisted 或 asyncore< /a>)

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))

python 文档非常好,我从那里选择了 mysend() 函数。

如果你正在做计算相关的工作,请查看 XML-RPC,Python 拥有这一切给你打包好了。

请记住,套接字就像文件一样,因此编写代码并没有太大区别,因此,只要您可以执行基本的文件 io 并理解事件,套接字编程一点也不难(只要你不会像复用 VoIP 流那样变得太复杂......)

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...)

仙女 2024-11-13 19:30:08

如果你完全不知道socket是什么,那么使用Twisted可能会有点困难。由于您需要识别正在传输的数据的类型,事情会变得更加困难。

因此也许ICE,互联网通信引擎的Python版本会更适合,因为它隐藏了一个网络编程的许多肮脏细节。看看你好世界看看它是否能完成你的工作。

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.

も让我眼熟你 2024-11-13 19:30:08

看这里:
如果您(正如我认为的那样)尝试使用套接字,这就是您正在寻找的:https://docs.python.org/2/howto/sockets.html

我希望这会有所帮助,因为它对我来说效果很好。
或者添加此类以实现持续连接:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文