温馨提示×

linux python脚本 数据传输咋办

小樊
81
2024-11-29 19:52:01
栏目: 编程语言

在Linux环境下,使用Python进行数据传输可以通过多种方式实现。以下是一些常见的方法:

  1. 使用socket库进行TCP/UDP通信:
import socket

# 创建TCP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(5)

while True:
    client_socket, addr = server_socket.accept()
    data = client_socket.recv(1024)
    print("Received data:", data.decode())
    client_socket.sendall(data)
    client_socket.close()

客户端代码:

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 12345))
client_socket.sendall(b'Hello, Server!')
data = client_socket.recv(1024)
print("Received data:", data.decode())
client_socket.close()
  1. 使用requests库进行HTTP请求:

首先安装requests库:

pip install requests

发送GET请求:

import requests

url = 'https://api.example.com/data'
response = requests.get(url)
print("Received data:", response.text)

发送POST请求:

import requests

url = 'https://api.example.com/data'
data = {'key': 'value'}
response = requests.post(url, json=data)
print("Received data:", response.text)
  1. 使用kafka-python库进行Kafka消息传输:

首先安装kafka-python库:

pip install kafka-python

生产者代码:

from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('my_topic', key=b'my_key', value=b'my_value')
producer.flush()

消费者代码:

from kafka import KafkaConsumer

consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='my_group')
for msg in consumer:
    print("Received data:", msg.value.decode())
  1. 使用pyarrow库进行Parquet文件传输:

首先安装pyarrow库:

pip install pyarrow

将数据保存为Parquet文件:

import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq

data = {'column1': [1, 2, 3], 'column2': ['A', 'B', 'C']}
df = pd.DataFrame(data)
table = pa.Table.from_pandas(df)
pq.write_table(table, 'data.parquet')

从Parquet文件读取数据:

import pandas as pd
import pyarrow.parquet as pq

table = pq.read_table('data.parquet')
df = table.to_pandas()
print("Received data:", df)

这些方法可以帮助你在Linux环境下使用Python进行数据传输。你可以根据自己的需求选择合适的方法。

0