python asynchronous httprequest

2024/10/13 13:23:07

I am trying to use twitter search web service in python. I want to call a web service like:

http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed

from my python program.

Can anybody tell me

  1. how to use xmlhttprequst object in python

  2. how to pass parameters to it, and

  3. how to get the data in dictionary.

Here is my try:

import urllib
import sys
url = "http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed"
urlobj = urllib.urlopen(url)
data = urlobj.read()
print data

Thanks.

Answer

You don't need "asynchronous httprequest" to use twitter search api:

import json
import urllib
import urllib2# make query
query = urllib.urlencode(dict(q="blue angel", rpp=5, include_entities=1,result_type="mixed"))  
# make request
resp = urllib2.urlopen("http://search.twitter.com/search.json?" + query)# make dictionary (parse json response)
d = json.load(resp)

There are probably several libraries that provide a nice OO interface around these http requests.

To make multiple requests concurrently you could use gevent:

import gevent
import gevent.monkey; gevent.monkey.patch_all() # patch stdlibimport json
import urllib
import urllib2def f(querystr):query = urllib.urlencode(dict(q=querystr, rpp=5, include_entities=1,result_type="mixed"))resp = urllib2.urlopen("http://search.twitter.com/search.json?" + query)d = json.load(resp)print('number of results %d' % (len(d['results']),))jobs = [gevent.spawn(f, q) for q in ['blue angel', 'another query']]
gevent.joinall(jobs) # wait for completion
https://en.xdnf.cn/q/69532.html

Related Q&A

What are response codes for 256 and 512 for os.system in python scripting

When i ping servers with os.system in python i get multiple response codes. Command used - os.system("ping -q -c 30 -s SERVERANME")0 - Online 256 - Offline 512 - what does 512 mean ?

Sphinx floating point formatting

Im using Sphinx to generate documentation from code. Does anyone know if there is a way to control the formatting of floating point numbers generated from default arguments. For example if I have the f…

Truncating column width in pandas

Im reading in large csv files into pandas some of them with String columns in the thousands of characters. Is there any quick way to limit the width of a column, i.e. only keep the first 100 characters…

Django - CreateView with multiple models

Can I use Django CreateViews to make a form that add data to multiple tables? Ive created a model called UserMeta to store some additional informations of my users. The ProblemI want to create a view …

Is there a way to pass dictionary in tf.data.Dataset w/ tf.py_func?

Im using tf.data.Dataset in data processing and I want to do apply some python code with tf.py_func.BTW, I found that in tf.py_func, I cannot return a dictionary. Is there any way to do it or workaroun…

How to split only on carriage returns with readlines in python?

I have a text file that contains both \n and \r\n end-of-line markers. I want to split only on \r\n, but cant figure out a way to do this with pythons readlines method. Is there a simple workaround for…

Python + MySQLdb executemany

Im using Python and its MySQLdb module to import some measurement data into a Mysql database. The amount of data that we have is quite high (currently about ~250 MB of csv files and plenty of more to c…

How to popup success message in odoo?

I am sending invitation by clicking button after clicking button and successfully sending invitation there is pop up message of successfully invitation send. But the problem is that the main heading of…

How to make ttk.Scale behave more like tk.Scale?

Several Tk widgets also exist in Ttk versions. Usually they have the same general behaviour, but use "styles" and "themes" rather than per-instance appearance attributes (such as bg…

pandas cut multiple columns

I am looking to apply a bin across a number of columns.a = [1, 2, 9, 1, 5, 3] b = [9, 8, 7, 8, 9, 1]c = [a, b]print(pd.cut(c, 3, labels=False))which works great and creates:[[0 0 2 0 1 0] [2 2 2 2 2 0]…