Asyncore Python Module Rest calls

Asyncore Python Module Rest calls

Postby chandrani » Wed Aug 10, 2016 7:16 am

Hi,
I want to use asyncore module in python for making a Rest call (GET) having a Token and few header parameters.

Can anyone suggest how do i bundle the header information and token and make a REST call using Asyncore.
chandrani
 
Posts: 6
Joined: Fri Jul 22, 2016 9:45 am

Re: Asyncore Python Module Rest calls

Postby Ofnuts » Wed Aug 10, 2016 9:26 am

Some exemples here.

But why would you want do to that? Using the requests module in a multi-thread program would be just as efficient and a lot simpler.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Asyncore Python Module Rest calls

Postby chandrani » Fri Aug 12, 2016 11:46 am

Hi,
Thanks for the link. It has helped but still it throws GENERIC_HTTP_400 bad request. I am pasting my code here. could you please point out the error in the request that I am sending.

import asyncore
import logging
import socket
from io import StringIO
from urllib.parse import urlparse
import string
import time
import ssl

class HttpClient(asyncore.dispatcher):

def __init__(self, url):
self.url = url
self.file = "output.tar.gz"
self.logger = logging.getLogger(self.url)
scheme, host, path, params, query, fragment = urlparse(url)
#host, port = string.split(host, ":", 1)
host = 'xx.xxx.xxx.xxx'
port = '443'
port = int(port)
asyncore.dispatcher.__init__(self)
header1 ="Auth:LTUzNzk2MzMzMDk5tpqOSIxKUZCUhGO-jra2crckP42B8OSS"
header2 ="X-Api-Version:300"
header3 ="Content-type:Application/json"
header4="Accept:*/*"
self.write_buffer = "GET %s HTTP/1.1\r\nHost:%s\r\n%s\r\n%s\r\n%s\r\n%s\r\n\r\n" % (path, host,header1,header2,header3,header4)
#self.write_buffer = "GET %s HTTP/1.1\r\nHost:%s\r\n\r\n" % (path, host)
a = self.write_buffer.encode()
print("\n The decoded request is =%s"%a.decode())
print("\n Wrute Buffer = \n%s"%self.write_buffer)
self.read_buffer = StringIO()
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.header = None
self.connect((host, port))
time.sleep(10)

def handle_connect(self):
print("\n Connected now asking for resource")
#self.socket = ssl.wrap_socket(self.socket,certfile=None,cert_reqs=CERT_NONE)
#self.send (self.write_buffer.encode())
#time.sleep(10)
#self.logger.debug('handle_connect()')
#print("\n Connected")

def handle_close(self):
self.logger.debug('handle_close()')
print("\n Close =%s"%self.header)
self.close()

def handle_error(self):
print("handle_error error!")
self.close()

def writable(self):
#is_writable = (len(self.write_buffer) > 0)
#if is_writable:
# self.logger.debug('writable() -> %s', is_writable)
return True

def readable(self):
self.logger.debug('readable() -> True')
return True

def handle_write(self):
#print("\n Writing")
sent = self.send(self.write_buffer.encode())
#self.logger.debug('handle_write() -> "%s"', self.write_buffer[:sent])
self.write_buffer = self.write_buffer[sent:]

def handle_read(self):
data = self.recv(8192)
time.sleep(10)
print("\n Header =%s"%self.header)
self.logger.debug('handle_read() -> %d bytes', len(data))
print("Reading =%s"%data.decode())
#self.read_buffer.write(data.decode())
#print(self.read_buffer)
#with open("output.tar.gz", "w") as text_file:
# text_file.write(data.decode())

if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(name)s: %(message)s',
)

clients = [
HttpClient('https://xx.xxx.xxx.xxx/rest/appliance/support-dumps/pppppp.sdmp/')
]

logging.debug('LOOP STARTING')

asyncore.loop()

logging.debug('LOOP DONE')

for c in clients:
response_body = c.read_buffer.getvalue()
print(response_body)
chandrani
 
Posts: 6
Joined: Fri Jul 22, 2016 9:45 am

Re: Asyncore Python Module Rest calls

Postby Ofnuts » Fri Aug 12, 2016 5:22 pm

"BAd request" means that your HTTP request isn't compliant with the protocol. This is the price to pay when working with sockets, you have to implement a potentially complex protocol correctly. Your problem then isn't Python but understanding the fine points of the HTTP protocol, for which this is the wrong forum. Which is why I suggested to use "requests" with threads instead...
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to Networking

Who is online

Users browsing this forum: No registered users and 2 guests