Python - 将整数或字符串发送到Spark-Streaming

时间:2017-12-09 07:57:13

标签: python sockets apache-spark pyspark spark-streaming

我可以通过CSV文件发送数据。首先,将我的随机数写入CSV文件然后发送,但是可以直接发送吗? 我的套接字代码:

FB.api('/person_id_obtained_from_above/likes')
火花流媒体代码:

import socket
host = 'localhost'
port = 8080

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
while True:
    print('\nListening for a client at',host , port)
    conn, addr = s.accept()
    print('\nConnected by', addr)
    try:
        print('\nReading file...\n')
        while 1:
            out = "test01"
            print('Sending line', line)
            conn.send(out)
    except socket.error:
        print ('Error Occured.\n\nClient disconnected.\n')
conn.close()

1 个答案:

答案 0 :(得分:0)

这是因为套接字阻塞发送数据并且永远不会继续。最基本的解决方案是发送一些数据并关闭连接:

import socket
import time

host = 'localhost'
port = 50007

i = 0

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

s.bind((host, port))
s.listen(1)

try:
    while True:
        conn, addr = s.accept()
        try:
            for j in range(10):
                conn.send(bytes("{}\n".format(i), "utf-8"))
                i += 1
                time.sleep(1)
            conn.close()
        except socket.error: pass
finally:
    s.close()

要获得更有趣的内容,请检查非阻塞模式是否超时。