I'm trying to stream m4a file over http using a simple wsgi server in python.
My code is like this:
mp3file = urllib2.urlopen(media_url)
data = mp3file.read()
headers = [('Content-type', 'audio/mp4'), ('Accept-Ranges', 'bytes')]
write = start_response('200 OK', headers)
write(data)
The problem is that if I set 'Content-Length' header like below, the client (Chrome browser for example) doesn't play the media. And if I don't set this header, it does play the media, but it doesn't know the length of the media so I can't seek/fast forward over the media file.
headers = [('Content-type', 'audio/mp4'), ('Content-Length', mp3file.headers['Content-Length']), ('Accept-Ranges', 'bytes')]
When setting the header I get this on console:
Traceback (most recent call last):
File "C:\Python27\lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "C:\Python27\lib\wsgiref\handlers.py", line 128, in finish_response
self.write(data)
File "C:\Python27\lib\wsgiref\handlers.py", line 217, in write
self._write(data)
File "C:\Python27\lib\socket.py", line 324, in write
self.flush()
File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
127.0.0.1 - - [28/Sep/2015 16:37:20] "GET /?yb=QfCqNbzJi3k HTTP/1.1" 500 59
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 653, in __init__
self.finish()
File "C:\Python27\lib\SocketServer.py", line 712, in finish
self.wfile.close()
File "C:\Python27\lib\socket.py", line 279, in close
self.flush()
File "C:\Python27\lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
Related
I am trying to play a video on an iOS device. When I tried to play i got the following error:
[27/Apr/2015 06:59:30] "GET /media/2015/04/VID_20150327_112644.mp4 HTTP/1.1" 200 18
Exception happened during processing of request from ('192.168.1.230', 51412)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/likewise-open/ZEALOUSYS/manesh/virtualenv/tracks/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('192.168.1.230', 51412)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/home/likewise-open/ZEALOUSYS/manesh/virtualenv/tracks/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
self.wfile.close()
File "/usr/lib/python2.7/socket.py", line 279, in close
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
The video file of all formats is working perfectly in all browsers and Android devices. When I try to access it from an iOS device. It gives me the above error. As you can see in error, first it is giving me response as 200 and then there is broken pipe. What does this broken pipe even mean? Just FYI, I have already posted a question stating that I can't play video on iOS devices, I am now posting a new question because I found this error in my terminal.
How do i fix this issue? Any help is appreciated.
This is beacause django is handling your media files not apache.Try by changing you configuration in server.
For example inside your apache2 edit example.com.conf file add following lines so that apache will serve media files.
**Alias /media /your project path/media**
Sometimes this may fix this issue.
def jsonCatch(environ,start_response):
results = requests.get("http://localhost:8055/jsonResponse")
start_response('200 OK', [('Content-Type', 'application/json')])
return results.json()
from wsgiref.simple_server import make_server
httpd = make_server('', 8050, application)
print('Serving on port 8050...')
httpd.serve_forever()
serving on port 8055
def jsonResponse(environ,start_response):
responseData={}
responseData['name']="alex"
responseData['age']="12"
start_response('200 OK',[('Content-Type', 'application/json')])
return json.dumps(responseData)
[ERROR]
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 86, in run
self.finish_response()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 128, in finish_response
self.write(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 204, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string
127.0.0.1 - - [07/Apr/2015 09:50:04] "GET /jsonCatch HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 52274)
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 124, in handle
handler.run(self.server.get_app())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 92, in run
self.close()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/simple_server.py", line 33, in close
self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
Edit: Changed https to http and error on server
I am trying to access a localhost server which returns a json response and is used by another localhost server. Can somebody show me an example of it?
I have changed the above code to
n=json.loads(results.text)
start_response('200 OK', [('Content-Type', 'text/plain')])
return n['name'].encode('utf-8')
and it returns me "alex"
try using http:// instead of https:// on this line: results = requests.get("https://localhost:8055/jsonResponse")
I have a very simple API server built on top of bottle. What is does is to just return a JSON upon being called, the content of the JSON being a random string. It works great.
This server was scanned yesterday by a vulnerability scanner (nessus) and crashed because of a misbehavior of the scanner. I include the Traceback information below which shows a error: [Errno 10054] An existing connection was forcibly closed by the remote host exception.
My question is the following: who is the one who should handle this problem?
Is this nessus, which does unholy things? On the one hand yes, it should behave in a standard way (this is a scanner which, among others, is used for industrial scans where it meets all sorts of devices and should attempt to mitigate the risk of crashes). On the other hand life is brutal and the receiving end (what is scanned) should be able to cope.
Is this bottle ? It is a framework which should handle typical and not so typical web traffic and leave to the programmer the task of handling the business logic and not the internals
Is this me? handling all the possible exceptions
I am asking as I would like to help improve either nessus or bottle (or myself) - this is not strictly a programming problem ("how to catch the exception") so if you really feel so, vote to close as opinion-based, though I ask anyway as I see it as a "programmers methodology" question.
The error stack:
C:\Python27\python.exe C:/Users/yop/Documents/dev/infoscreen/webserver.py
10.81.163.129 - - [03/Feb/2015 18:21:00] code 400, message Bad request syntax ('\x16\x03\x01\x00\xea\x01\x00\x00\xe6\x03\x01T\xd1\x03|\xb3\x02lm\xf0\xb92\x9a\xd7\xa7\xec\xbca\xdaR9\xfc\xe3]);`\xfbI\x98\x17\x94\xe3\x00\x00x\xc0\x14\xc0')
10.81.163.129 - - [03/Feb/2015 18:21:05] code 400, message Bad HTTP/0.9 request type ('\x16\x03\x01\x00\xea\x01\x00\x00\xe6\x03\x01T\xd1\x03\x81\xfcxI\x0e\xd5\xc4\xba\x17\xd9\x92\x9f+\x83BR\x10\x94N\xa5\x12\xc0')
10.81.163.129 - - [03/Feb/2015 18:21:24] code 400, message Bad request version ('Secure-HTTP/1.4')
10.81.163.129 - - [03/Feb/2015 18:21:24] code 400, message Bad request syntax ("\x16\x03\x01\x00\xea\x01\x00\x00\xe6\x03\x01T\xd1\x03\x95\x95\x154\x94h/5\x8dL\xa6\x148\x04#s\x13\x9f'\xec\x1az\xbc\x87/L\x0e\xc7\x02\x00\x00x\xc0\x14\xc0")
Traceback (most recent call last):
----------------------------------------
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
Exception happened during processing of request from ('10.81.163.129', 57035)
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 651, in __init__
self.handle()
File "C:\Python27\lib\wsgiref\simple_server.py", line 117, in handle
if not self.parse_request(): # An error code has been sent, just exit
File "C:\Python27\lib\BaseHTTPServer.py", line 291, in parse_request
self.headers = self.MessageClass(self.rfile, 0)
File "C:\Python27\lib\mimetools.py", line 25, in __init__
----------------------------------------
rfc822.Message.__init__(self, fp, seekable)
File "C:\Python27\lib\rfc822.py", line 108, in __init__
self.readheaders()
File "C:\Python27\lib\rfc822.py", line 155, in readheaders
line = self.fp.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Traceback (most recent call last):
----------------------------------------
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
Exception happened during processing of request from ('10.81.163.129', 37539)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
----------------------------------------
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 651, in __init__
self.handle()
File "C:\Python27\lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
----------------------------------------
Traceback (most recent call last):
Exception happened during processing of request from ('10.81.163.129', 38322)
File "C:\Python27\lib\SocketServer.py", line 295, in _handle_request_noblock
----------------------------------------
self.process_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 651, in __init__
self.handle()
File "C:\Python27\lib\wsgiref\simple_server.py", line 116, in handle
self.raw_requestline = self.rfile.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Process finished with exit code -1
I had a create a REST API using bottle framework, which receives calls from GAE. Once this REST API is invoked, it did some calculations and sent outputs as a zip file to AMAZON S3 server and return the link to GAE. Everything works fine expect the timeout issue. I tried to adjust the deadline of urlfetch to 60 seconds, which did not solve the problem. I appreciate any suggestions.
GAE side:
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=http_headers, deadline=60)
Broser error info.:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "D:\Dropbox\ubertool_src\przm5/przm5_output.py", line 22, in post
przm5_obj = przm5_rest_model.przm5(args)
File "D:\Dropbox\ubertool_src\przm5\przm5_rest_model.py", line 351, in __init__
self.convertSoil1, self.convert1to3, self.convert2to3)
File "D:\Dropbox\ubertool_src\przm5\przm5_rest_model.py", line 135, in get_jid
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=http_headers)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 270, in fetch
return rpc.get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 612, in get_result
return self.__get_result_hook(self)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\urlfetch.py", line 410, in _get_fetch_result
'Deadline exceeded while waiting for HTTP response from URL: ' + url)
DeadlineExceededError: Deadline exceeded while waiting for HTTP response from URL: http://localhost:7777/my_model
REST server:
#route('/my_model', method='POST')
#auth_basic(check)
def my_model():
#run the model
run_my model()
#zip output files
zout=zipfile.ZipFile("test.zip","w")
for name in os.listdir(src1):
zout.write(name)
zout.close()
##upload file to S3
conn = S3Connection(key, secretkey)
bucket = Bucket(conn, 'przm5')
k=Key(bucket)
name1='PRZM5_'+name_temp+'.zip'
k.key=name1
###All the above steps are fine####
k.set_contents_from_filename('test.zip')
link='https://s3.amazonaws.com/'+my_file_path
return {'ff': ff}
run(host='localhost', port=7777, debug=True)
Errors from the REST server:
127.0.0.1 - - [07/Jan/2014 16:16:36] "POST /my_model HTTP/1.1" 200 1663
Traceback (most recent call last):
File "C:\Python27\Lib\wsgiref\handlers.py", line 86, in run
self.finish_response()
File "C:\Python27\Lib\wsgiref\handlers.py", line 128, in finish_response
self.write(data)
File "C:\Python27\Lib\wsgiref\handlers.py", line 212, in write
self.send_headers()
File "C:\Python27\Lib\wsgiref\handlers.py", line 270, in send_headers
self.send_preamble()
File "C:\Python27\Lib\wsgiref\handlers.py", line 194, in send_preamble
'Date: %s\r\n' % format_date_time(time.time())
File "C:\Python27\Lib\socket.py", line 324, in write
self.flush()
File "C:\Python27\Lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
127.0.0.1 - - [07/Jan/2014 16:16:36] "POST /my_model HTTP/1.1" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50953)
Traceback (most recent call last):
File "C:\Python27\Lib\SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "C:\Python27\Lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\Lib\SocketServer.py", line 651, in __init__
self.finish()
File "C:\Python27\Lib\SocketServer.py", line 710, in finish
self.wfile.close()
File "C:\Python27\Lib\socket.py", line 279, in close
self.flush()
File "C:\Python27\Lib\socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
The deadline is a maximum value, once reached it'll fail. And it's failing with
Deadline exceeded while waiting for HTTP response
So you should try to catch that exception and try again.
If the entire operation can't be done in under 60 seconds then there is nothing else to be done, it's a hard limit in GAE that HTTP requests can't exceed 60 seconds.
Here is the code:
conn = httplib.HTTPConnection("127.0.0.1:8000")
conn.request("POST", "/api/job/", some_params, headers)
conn.close()
no problem with sending request to server
but if i use loop for example:
for i in range(n):
conn = httplib.HTTPConnection("127.0.0.1:8000")
conn.request("POST", "/api/job/", some_params, headers)
conn.close()
it rises an exception, but it is interesting that request is successfull:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 284, in run
self.finish_response()
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 324, in finish_response
self.write(data)
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 403, in write
self.send_headers()
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 467, in send_headers
self.send_preamble()
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 385, in send_preamble
'Date: %s\r\n' % http_date()
File "/usr/lib/python2.7/socket.py", line 324, in write
self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 60438)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 284, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 310, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 570, in __init__
BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
any suggestions ???
Looks to me like your buffer is getting filled. Your buffer will fill with any network requests you make, then is cleared when the server acknowledges receipt of the data. Not sure if there isn't a better way to do this, but you could try giving the server some time to acknowledge receipt by doing a short time.pause within your loop.