-
-
Save saeedaghabozorgi/6c3872a6fdde18197c04 to your computer and use it in GitHub Desktop.
Created with ❤️ on Data Scientist Workbench (https://datascientistworkbench.com/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import socket\n", | |
"import sys\n", | |
"from thread import *\n", | |
"import requests\n", | |
"import requests_oauthlib\n", | |
"import json\n", | |
"import oauth2 as oauth\n", | |
"from datetime import datetime\n", | |
"import time" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"#Variables that contains the user credentials to access Twitter API\n", | |
"\n", | |
"auth = requests_oauthlib.OAuth1(consumer_key, consumer_secret,access_token, access_token_secret)\n", | |
"\n", | |
"\n", | |
"\n", | |
"#Function for handling connections. This will be used to read data from tweeter and write to socket\n", | |
"def clientthread(conn):\n", | |
" url='https://stream.twitter.com/1.1/statuses/filter.json'\n", | |
" #\n", | |
" data = [('language', 'en'), ('locations', '-130,-20,100,50')]\n", | |
" #,('track','ibm,google,microsoft')\n", | |
" query_url = url + '?' + '&'.join([str(t[0]) + '=' + str(t[1]) for t in data])\n", | |
" response = requests.get(query_url, auth=auth, stream=True)\n", | |
" print(query_url, response) # 200 <OK>\n", | |
" count = 0\n", | |
" for line in response.iter_lines(): # Iterate over streaming tweets\n", | |
" try:\n", | |
" if count > 10000000:\n", | |
" break\n", | |
" post= json.loads(line.decode('utf-8'))\n", | |
" #contents = [post['text'], post['coordinates'], post['place']]\n", | |
" count+= 1\n", | |
" conn.send(line+'\\n')\n", | |
" #time.sleep(1)\n", | |
"\n", | |
" print (str(datetime.now())+' '+'count:'+str(count))\n", | |
" except:\n", | |
" e = sys.exc_info()[0]\n", | |
" print( \"Error: %s\" % e )\n", | |
" conn.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Socket created\n", | |
"Socket bind complete\n", | |
"Socket now listening\n", | |
"Connected with 127.0.0.1:41890" | |
] | |
} | |
], | |
"source": [ | |
"HOST = '' \n", | |
"PORT = 9994 # Arbitrary non-privileged port\n", | |
"\n", | |
"s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", | |
"print 'Socket created'\n", | |
"\n", | |
"#Bind socket to local host and port\n", | |
"try:\n", | |
" s.bind((HOST, PORT))\n", | |
"except socket.error , msg:\n", | |
" print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]\n", | |
" sys.exit()\n", | |
"\n", | |
"print 'Socket bind complete'\n", | |
"\n", | |
"#Start listening on socket\n", | |
"s.listen(10)\n", | |
"print 'Socket now listening'\n", | |
"\n", | |
"\n", | |
"\n", | |
"#now keep talking with the client\n", | |
"while 1:\n", | |
" #wait to accept a connection - blocking call\n", | |
" conn, addr = s.accept()\n", | |
" print 'Connected with ' + addr[0] + ':' + str(addr[1])\n", | |
"\n", | |
" #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.\n", | |
" start_new_thread(clientthread ,(conn,))\n", | |
"\n", | |
"s.close()\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"%tb" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
hello
I tried this code but during the execution the code could not create the address and the connection. the program does not stop.
i need help please
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I LOVE THIS! God bless you for making my life easier! :)