errno 32尝试将tweet数据流传输到pyspark(使用tweepy)并将tweet数据发送到客户端时,tweet侦听器中出现断管错误

kulphzqa  于 2021-07-09  发布在  Spark
关注(0)|答案(0)|浏览(257)

我正在尝试用以下代码流式传输twitter数据:import tweepy from tweepy import oauthhandler#to authenticate tweepy api from tweepy import stream from tweepy.streaming import streamlistener import socket import json

  1. # Twitter developer Credentials to connect to twitter account
  2. access_token = "access token"
  3. access_secret = "access secret"
  4. consumer_key = "consumer key"
  5. consumer_secret = "consumer secret"
  6. class TweetsListener(StreamListener):
  7. # initialized the constructor
  8. def __init__(self, csocket):
  9. self.client_socket = csocket
  10. def on_data(self, data):
  11. try:
  12. # read the Twitter data which comes as a JSON format
  13. msg = json.loads(data)
  14. # the 'text' in the JSON file contains the actual tweet.
  15. print(msg['text'].encode('utf-8'))
  16. # the actual tweet data is sent to the client socket
  17. self.client_socket.send(msg['text'].encode('utf-8'))
  18. print("4")
  19. return True
  20. except BaseException as e:
  21. # Error handling
  22. print("Ahh! Look what is wrong : %s" % str(e))
  23. return True
  24. def on_error(self, status):
  25. print(status)
  26. return True
  27. def sendData(c_socket):
  28. # authentication
  29. auth = OAuthHandler(consumer_key, consumer_secret)
  30. auth.set_access_token(access_token, access_secret)
  31. # twitter_stream will get the actual live tweet data
  32. twitter_stream = Stream(auth, TweetsListener(c_socket))
  33. # filter the tweet feeds related to "corona"
  34. twitter_stream.filter(track=['marvel'])
  35. # in case you want to pass multiple criteria
  36. # twitter_stream.filter(track=['DataScience','python','Iot'])
  37. # create a socket object
  38. s = socket.socket()
  39. # Get local machine name : host and port
  40. host = "127.0.0.1"
  41. port = 7777
  42. # Bind to the port
  43. s.bind((host, port))
  44. print("Listening on port: %s" % str(port))
  45. # Wait and Establish the connection with client.
  46. s.listen(5)
  47. c, addr = s.accept()
  48. print("Received request from: " + str(addr))
  49. sendData(c)

我知道print“4”语句没有打印是因为上面的语句,而向socket发送tweet数据是导致错误的原因,我无法确定原因。有人能帮我吗?提前谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题