python-messaging Failed to handle HTTP request
Asked Answered
S

1

7

I am using the code below to try to send an MMS message with python-messaging https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/mms.rst Although the connection seems to go smoothly I get the following response from the mmsc:

PROXY RESPONSE HTTP/1.0 200 OK
content-type: application/vnd.wap.mms-message
content-length: 59
Connection: close
Date: Sat, 05 Jan 2019 16:36:44 GMT
Server: Mavenir Web Application Server

���1234�����,�Failed to handle HTTP request in Mm1Server

Does, anyone have an idea on what the problem might be and how I can fix it? Here is my code:

from messaging.mms.message import MMSMessage, MMSMessagePage

mms = MMSMessage()
mms.headers['To'] = '+212XXXXXXX/TYPE=PLMN'
mms.headers['Message-Type'] = 'm-send-req'
mms.headers['Subject'] = 'Test python-messaging.mms'

slide1 = MMSMessagePage()
slide1.add_image('/home/richard/screensaver/TolleConscQte.jpg')
slide1.add_text('This first slide, is a step towards enlightenment.')

slide2 = MMSMessagePage()
slide2.set_duration(4500)
slide2.add_image('/home/richard/screensaver/TollePastALL.jpg', 1500)
slide2.add_text('This second slide is a second step towards enlightenment.', 500, 3500)

mms.add_page(slide1)
mms.add_page(slide2)

payload = mms.encode()

## sending the MMS

from cStringIO import StringIO
import socket

gw_host, gw_port = "10.188.239.143", 80 #ting

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((gw_host, gw_port))
s.send("POST %s HTTP/1.0\r\n" % "http://wholesale.mmsmvno.com/mms/wapenc")
s.send("Content-Type: application/vnd.wap.mms-message\r\n")
s.send("Content-Length: %d\r\n\r\n" % len(payload))

s.sendall(payload)

buf = StringIO()

while True:
    data = s.recv(4096)
    if not data:
        break

    buf.write(data)

s.close()
data = buf.getvalue()
buf.close()

print "PROXY RESPONSE", data
Sailcloth answered 5/1, 2019 at 16:52 Comment(10)
Have you tried adding a Host header? And is the server you reach really an explicit HTTP proxy (not a reverse proxy)? Otherwise the full URL should not be contained after the method in the request but only the path component.Augie
Thanks for the reply. I'm not sure how to add a host header but additionally I do not know if it an explicit proxy or a reverse proxy. Are you saying that the post line should be s.send("POST %s HTTP/1.0\r\n" % "/mms/wapenc") ?Sailcloth
Correct. And the host header should be added like the others, i.e. s.send("Host: wholesale.mmsmvno.com\r\n")Augie
Thanks, but when I try: s.send("Host: wholesale.mmsmvno.com\r\n") s.send("POST %s HTTP/1.0\r\n" % "/mms/wapenc") s.send("Content-Type: application/vnd.wap.mms-message\r\n") s.send("Content-Length: %d\r\n\r\n" % len(payload)) I get: Traceback (most recent call last): File "test_sendMMS.py", line 42, in <module> s.sendall(payload) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 32] Broken pipeSailcloth
A HTTP request consists of the request-line (GET ... etc) followed by lines with key: value pairs etc - you have to send the parts of the request in the correct order and not send the Host header before you send the request-line. See for example here for details.Augie
So should s.send("Host: wholesale.mmsmvno.com\r\n") go after s.send("POST %s HTTP/1.0\r\n" % "/mms/wapenc") and before s.send("Content-Type: application/vnd.wap.mms-message\r\n") ?Sailcloth
This would be more correct.Augie
Looks like I'm back to square one. In that order I get: ���1234�����,�Failed to handle HTTP request in Mm1ServerSailcloth
I believe that this might be an encoding issue. Adding print, payload spits out data in this format: 250, 54, 236, 174, 205, 180, 118, 125, 106, 252, 213, 217, 183, 232, 250, 180, 125, 93, 181, 217, 217, 234, 167, 225, 216, 118, 87, 111, 213, 253, 170, 250, 123, 43, 255, 217, 1, 57, 131, 84, 104, 105, 115, 32, 115, 101, 99, 111, 110, 100, 32, 115, 108, 105, 100, 101 whereas the smil files I receive are in this format: V9#le+jC׌4}*U^ps5vl*<g߭M;zEcpwt<SSailcloth
maybe instead of handling the HTTP request via socket. try using requests module.link to docsAime
H
1

I ran into this same error. The error is with the encoded MMS PDU, not the http request. I was able to get this to work by explicitly setting the From header:

mms.headers['From'] = ''

This will cause the python-messaging library to set the From header to Insert-address-token.

I used this test file for debugging (just change the To phone number).

Its also fine to use a normal http client. Just make sure you are setting Content-Type and Content-Length.

Hospitium answered 27/8, 2023 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.