简体   繁体   English

通过Internet传输数据的最简单方法,Python

[英]Easiest Way to Transfer Data Over the Internet, Python

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. 两个系统都将运行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. 如果您喜欢抽象(或需要异步支持),那么总会有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 ) 这是套接字接口的一个例子(当你的程序变大时会越来越难用,所以我建议使用Twisted或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. python文档非常好,我从那里获取了mysend()函数。

If you are doing computation related work, check out XML-RPC , which python has all nicely packaged up for you. 如果您正在进行与计算相关的工作,请查看XML-RPC ,哪些python已经为您精心打包。

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...) 请记住,套接字就像文件一样,所以编写代码并没有太大的不同,所以,只要你能做基本文件io,并了解事件,套接字编程就不难了(只要你不会像复用VoIP流那样复杂......)

If you have completely no idea of what socket is, it might be a bit difficult to use Twisted. 如果你完全不知道什么是套接字,那么使用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. 所以也许是ICE的python版本,Internet通信引擎将更适合,因为它隐藏了很多网络编程的脏细节。 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 看看这里:如果您,我认为您正在尝试使用套接字,那么您正在寻找: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python通过互联网传输数据的最轻量级方法是什么? - What is the most lightweight way to transmit data over the internet using Python? 每天一次运行Python脚本(可访问Internet)的最简单方法? - Easiest way to run a Python script (that access the internet) once a day? 2 Python 脚本通过本地网络在 2 台计算机上共享数据的最简单、最简单的方法 - Simplest, easiest way for 2 Python scripts on 2 computers over local network to share data 在python中通过多个cpu核心运行简单循环(适用于不同数据)的最简单方法? - Easiest way to run simple loops (that work on different data) over multiple cpu cores in python? 在python中将数据结构持久化到文件的最简单方法? - Easiest way to persist a data structure to a file in python? 用python在国家地图上绘制数据的最简单方法 - Easiest way to plot data on country map with python 在 python 中打印数据头的最简单方法? - Easiest way to print the head of a data in python? 通过python套接字的数据传输不完整 - Incomplete data transfer over python socket 将 API 数据转换为 str/int 格式的最简单方法 python - Easiest way to get API data into str/int format python 在Python中从文件加载数组数据的最简单方法是什么? - what's the easiest way to load array data from a file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM