Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requests example for 2.0.0a3 #217

Closed
BRIDGE-AI opened this issue Jan 16, 2024 · 5 comments
Closed

requests example for 2.0.0a3 #217

BRIDGE-AI opened this issue Jan 16, 2024 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@BRIDGE-AI
Copy link

BRIDGE-AI commented Jan 16, 2024

Hello,

I'm trying to adapting 2.0.0a3 for the new feature 'transfer' from 1.6.6. #207
First of all, I've upgraded python to 3.8 from 3.7.
But it has quietly different interface like below.

import

from pyVoIP.VoIP.phone import VoIPPhone, VoIPPhoneParameter
from pyVoIP.credentials import CredentialsManager

create phone

    cm = CredentialsManager()
    cm.add(username=username, password=password)
    param = VoIPPhoneParameter(server=server, port=port, user=username, credentials_manager=cm)

    #phone=VoIPPhone(server_ip, port, username, password, callCallback=callback) # for 1.6
    phone = VoIPPhone(param)

but still I has some errors

Exception in thread SIP Register CSeq: 5:
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1291, in register
    registered = self.__register()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1341, in __register
    password_request = self.gen_register(response)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 647, in gen_register
    regRequest += self.gen_authorization(request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 493, in gen_authorization
    digest = self.gen_digest(request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 432, in gen_digest
    qop = request.authentication.get("qop", None).pop(0)
AttributeError: 'NoneType' object has no attribute 'pop'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 1254, in run
    self.function(*self.args, **self.kwargs)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1312, in register
    self.fatal_callback()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/VoIP/phone.py", line 313, in fatal
    self.stop(failed=True)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/VoIP/phone.py", line 306, in stop
    self.sip.stop()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 239, in stop
    self.deregister()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1210, in deregister
    deregistered = self.__deregister()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1232, in __deregister
    conn = self.send(first_request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 250, in send
    return self.s.send(request.encode("utf8"))
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/sock/sock.py", line 473, in send
    conn.send(data)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/sock/sock.py", line 68, in send
    self.sock.s.sendto(data, addr)
OSError: [Errno 9] Bad file descriptor

This server works fine on 1.6.6.

Is this right implementation? or does I missed some reference or document?

In conclusion,
may I ask you a new example code for 2.0 please? especially for transfer feature.

Thank you, always.

@tayler6000
Copy link
Owner

@BRIDGE-AI It looks like you are doing things correctly. Here is the documentation on this feature. Looking at this error it seems like there is a problem with the authentication function which has changed since 1.6. I'll see if I can diagnose the issue, but a packet capture would be very helpful. If you put the following at the beginning of your code it should make it fairly easy:

import pyVoIP

pyVoIP.DEBUG = True

@tayler6000 tayler6000 added the bug Something isn't working label Jan 16, 2024
@github-project-automation github-project-automation bot moved this to Todo - Release in pyVoIP Jan 16, 2024
@tayler6000 tayler6000 moved this from Todo - Release to Todo - Patching in pyVoIP Jan 16, 2024
@tayler6000 tayler6000 moved this from Todo - Patching to Todo - Release in pyVoIP Jan 16, 2024
@BRIDGE-AI
Copy link
Author

BRIDGE-AI commented Jan 17, 2024

Thank you, @tayler6000

I used the example code.

# https://pyvoip.readthedocs.io/en/development/Examples.html#ivr-phone-menus
import pyVoIP; pyVoIP.DEBUG = True

from pyVoIP.credentials import CredentialsManager
from pyVoIP.VoIP.call import VoIPCall
from pyVoIP.VoIP.error import InvalidStateError
from pyVoIP.VoIP.phone import VoIPPhone, VoIPPhoneParameter
import time
import wave

class Call(VoIPCall):

    def ringing(self, invite_request):
        try:
            f = wave.open('announcment.wav', 'rb')
            frames = f.getnframes()
            data = f.readframes(frames)
            f.close()

            call.answer()
            call.write_audio(data)  # This writes the audio data to the transmit buffer, this must be bytes.

            stop = time.time() + (frames / 8000)  # frames/8000 is the length of the audio in seconds. 8000 is the hertz of PCMU.

            while time.time() <= stop and call.state == CallState.ANSWERED:
                time.sleep(0.1)
            call.hangup()
        except InvalidStateError:
            pass
        except:
            call.hangup()

if __name__ == "__main__":
    server_ip = "211.233.26.247"
    port = 5060
    username = "07076830783"
    password = ""
    localip = "0.0.0.0"

    cm = CredentialsManager()
    #cm.add(<SIP server username>, <SIP server password>)
    cm.add(username, password)
    #params = VoIPPhoneParameter(<SIP server IP>, <SIP server port>, <SIP server user>, cm, bind_ip=<Your computer's local IP>, call_class=Call)
    params = VoIPPhoneParameter(server_ip, port, username, cm, bind_ip=localip, call_class=Call)
    phone = VoIPPhone(params)
    phone.start()
    input('Press enter to disable the phone')
    phone.stop()

And I've got below logs right after run the code.

$ python t_ex1_ann.py

SENT:
REGISTER sip:211.233.26.247:5060 SIP/2.0

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK1bfabb750d84446db8de8f4b0'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>', 'tag': '', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 1, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
Allow: ['INVITE', 'ACK', 'BYE', 'CANCEL', 'OPTIONS', 'NOTIFY']
Max-Forwards: 70
Allow-Events: org.3gpp.nwinitdereg
User-Agent: pyVoIP 2.0.0a3
Expires: 120
Content-Length: 0

Body:

Raw:
b'REGISTER sip:211.233.26.247:5060 SIP/2.0\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK1bfabb750d84446db8de8f4b0\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>\r\nCall-ID: [email protected]:5060\r\nCSeq: 1 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY\r\nMax-Forwards: 70\r\nAllow-Events: org.3gpp.nwinitdereg\r\nUser-Agent: pyVoIP 2.0.0a3\r\nExpires: 120\r\nContent-Length: 0\r\n\r\n'


Received UDP Message:
SIP/2.0 401 Unauthorized

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK1bfabb750d84446db8de8f4b0', 'received': '43.202.127.199'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c', 'tag': 'ebcb6c0114297d73a48fab2b598eb87c', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 1, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
WWW-Authenticate: {'header': 'WWW-Authenticate', 'method': 'Digest', 'realm': 'sip1.1stnet.co.kr', 'nonce': '6be5f220592a70d7f6bb463470a9509c', 'stale': 'true', 'algorithm': 'MD5'}
Content-Length: 0

Body:

Raw:
b'SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK1bfabb750d84446db8de8f4b0;received=43.202.127.199\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c\r\nCall-ID: [email protected]:5060\r\nCSeq: 1 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nWWW-Authenticate: Digest realm="sip1.1stnet.co.kr", nonce="6be5f220592a70d7f6bb463470a9509c", stale=true, algorithm=MD5\r\nContent-Length: 0\r\n\r\n'
New Connection Started
Deregistering <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d187970>
self.conns=[<pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d187970>]
listening: [["[email protected]:5060", "737237f8", "ebcb6c0114297d73a48fab2b598eb87c", 0]]

msgs: []


REGISTERATION ERROR: 'NoneType' object has no attribute 'pop'
New register thread
Press enter to disable the phoneSENT:
REGISTER sip:211.233.26.247:5060 SIP/2.0

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK2b486c9074274197944b83308'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>', 'tag': '', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 3, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
Allow: ['INVITE', 'ACK', 'BYE', 'CANCEL', 'OPTIONS', 'NOTIFY']
Max-Forwards: 70
Allow-Events: org.3gpp.nwinitdereg
User-Agent: pyVoIP 2.0.0a3
Expires: 120
Content-Length: 0

Body:

Raw:
b'REGISTER sip:211.233.26.247:5060 SIP/2.0\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK2b486c9074274197944b83308\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>\r\nCall-ID: [email protected]:5060\r\nCSeq: 3 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY\r\nMax-Forwards: 70\r\nAllow-Events: org.3gpp.nwinitdereg\r\nUser-Agent: pyVoIP 2.0.0a3\r\nExpires: 120\r\nContent-Length: 0\r\n\r\n'


Received UDP Message:
SIP/2.0 401 Unauthorized

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK2b486c9074274197944b83308', 'received': '43.202.127.199'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c', 'tag': 'ebcb6c0114297d73a48fab2b598eb87c', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 3, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
WWW-Authenticate: {'header': 'WWW-Authenticate', 'method': 'Digest', 'realm': 'sip1.1stnet.co.kr', 'nonce': '6be5f220592a70d7f6bb463470a9509c', 'stale': 'true', 'algorithm': 'MD5'}
Content-Length: 0

Body:

Raw:
b'SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK2b486c9074274197944b83308;received=43.202.127.199\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c\r\nCall-ID: [email protected]:5060\r\nCSeq: 3 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nWWW-Authenticate: Digest realm="sip1.1stnet.co.kr", nonce="6be5f220592a70d7f6bb463470a9509c", stale=true, algorithm=MD5\r\nContent-Length: 0\r\n\r\n'
New Connection Started
Deregistering <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d19c0d0>
self.conns=[None, <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d19c0d0>]
listening: [["[email protected]:5060", "737237f8", "ebcb6c0114297d73a48fab2b598eb87c", 1]]

msgs: []


REGISTERATION ERROR: 'NoneType' object has no attribute 'pop'
New register thread
SENT:
REGISTER sip:211.233.26.247:5060 SIP/2.0

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK253ddb5c2d3244148aa49dc4f'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>', 'tag': '', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 5, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
Allow: ['INVITE', 'ACK', 'BYE', 'CANCEL', 'OPTIONS', 'NOTIFY']
Max-Forwards: 70
Allow-Events: org.3gpp.nwinitdereg
User-Agent: pyVoIP 2.0.0a3
Expires: 120
Content-Length: 0

Body:

Raw:
b'REGISTER sip:211.233.26.247:5060 SIP/2.0\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK253ddb5c2d3244148aa49dc4f\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>\r\nCall-ID: [email protected]:5060\r\nCSeq: 5 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY\r\nMax-Forwards: 70\r\nAllow-Events: org.3gpp.nwinitdereg\r\nUser-Agent: pyVoIP 2.0.0a3\r\nExpires: 120\r\nContent-Length: 0\r\n\r\n'


Received UDP Message:
SIP/2.0 401 Unauthorized

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK253ddb5c2d3244148aa49dc4f', 'received': '43.202.127.199'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c', 'tag': 'ebcb6c0114297d73a48fab2b598eb87c', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 5, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
WWW-Authenticate: {'header': 'WWW-Authenticate', 'method': 'Digest', 'realm': 'sip1.1stnet.co.kr', 'nonce': '6be5f220592a70d7f6bb463470a9509c', 'stale': 'true', 'algorithm': 'MD5'}
Content-Length: 0

Body:

Raw:
b'SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK253ddb5c2d3244148aa49dc4f;received=43.202.127.199\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c\r\nCall-ID: [email protected]:5060\r\nCSeq: 5 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nWWW-Authenticate: Digest realm="sip1.1stnet.co.kr", nonce="6be5f220592a70d7f6bb463470a9509c", stale=true, algorithm=MD5\r\nContent-Length: 0\r\n\r\n'
New Connection Started
Deregistering <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d19c4c0>
self.conns=[None, None, <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d19c4c0>]
listening: [["[email protected]:5060", "737237f8", "ebcb6c0114297d73a48fab2b598eb87c", 2]]

msgs: []


REGISTERATION ERROR: 'NoneType' object has no attribute 'pop'
SENT:
REGISTER sip:211.233.26.247:5060 SIP/2.0

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK6cf32d1faff841fbb81246f7a'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>', 'tag': '', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 7, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
Allow: ['INVITE', 'ACK', 'BYE', 'CANCEL', 'OPTIONS', 'NOTIFY']
Max-Forwards: 70
Allow-Events: org.3gpp.nwinitdereg
User-Agent: pyVoIP 2.0.0a3
Expires: 0
Content-Length: 0

Body:

Raw:
b'REGISTER sip:211.233.26.247:5060 SIP/2.0\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK6cf32d1faff841fbb81246f7a\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>\r\nCall-ID: [email protected]:5060\r\nCSeq: 7 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nAllow: INVITE, ACK, BYE, CANCEL, OPTIONS, NOTIFY\r\nMax-Forwards: 70\r\nAllow-Events: org.3gpp.nwinitdereg\r\nUser-Agent: pyVoIP 2.0.0a3\r\nExpires: 0\r\nContent-Length: 0\r\n\r\n'


Received UDP Message:
SIP/2.0 401 Unauthorized

Headers:
Via: [{'type': 'SIP/2.0/UDP', 'address': ('0.0.0.0', 5060), 'branch': 'z9hG4bK6cf32d1faff841fbb81246f7a', 'received': '43.202.127.199'}]
From: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=737237f8', 'tag': '737237f8', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '0.0.0.0', 'port': 5060}
To: {'raw': '"07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c', 'tag': 'ebcb6c0114297d73a48fab2b598eb87c', 'uri': 'sip:[email protected]:5060', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '07076830783', 'host': '211.233.26.247', 'port': 5060}
Call-ID: [email protected]:5060
CSeq: {'check': 7, 'method': 'REGISTER'}
Contact: {'raw': '<sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"', 'tag': '', 'uri': 'sip:[email protected]', 'uri-type': 'sip', 'user': '07076830783', 'password': '', 'display-name': '', 'host': '0.0.0.0', 'port': 5060}
WWW-Authenticate: {'header': 'WWW-Authenticate', 'method': 'Digest', 'realm': 'sip1.1stnet.co.kr', 'nonce': '6be5f220592a70d7f6bb463470a9509c', 'stale': 'true', 'algorithm': 'MD5'}
Content-Length: 0

Body:

Raw:
b'SIP/2.0 401 Unauthorized\r\nVia: SIP/2.0/UDP 0.0.0.0:5060;branch=z9hG4bK6cf32d1faff841fbb81246f7a;received=43.202.127.199\r\nFrom: "07076830783" <sip:[email protected]:5060>;tag=737237f8\r\nTo: "07076830783" <sip:[email protected]:5060>;tag=ebcb6c0114297d73a48fab2b598eb87c\r\nCall-ID: [email protected]:5060\r\nCSeq: 7 REGISTER\r\nContact: <sip:[email protected];transport=UDP>;+sip.instance="<urn:uuid:884AEEEF-0A56-48F7-9327-AE2E5813F844>"\r\nWWW-Authenticate: Digest realm="sip1.1stnet.co.kr", nonce="6be5f220592a70d7f6bb463470a9509c", stale=true, algorithm=MD5\r\nContent-Length: 0\r\n\r\n'
New Connection Started
Deregistering <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d187f10>
self.conns=[None, None, None, <pyVoIP.sock.sock.VoIPConnection object at 0x7fa85d187f10>]
listening: [["[email protected]:5060", "737237f8", "ebcb6c0114297d73a48fab2b598eb87c", 3]]

msgs: []


DEREGISTERATION ERROR: 'NoneType' object has no attribute 'pop'
Error unpacking data, only using header: not enough values to unpack (expected 2, got 1)
DEREGISTERATION ERROR: [Errno 9] Bad file descriptor
Exception in thread SIP Register CSeq: 5:
Traceback (most recent call last):
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1291, in register
    registered = self.__register()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1341, in __register
    password_request = self.gen_register(response)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 647, in gen_register
    regRequest += self.gen_authorization(request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 493, in gen_authorization
    digest = self.gen_digest(request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 432, in gen_digest
    qop = request.authentication.get("qop", None).pop(0)
AttributeError: 'NoneType' object has no attribute 'pop'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 1254, in run
    self.function(*self.args, **self.kwargs)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1312, in register
    self.fatal_callback()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/VoIP/phone.py", line 313, in fatal
    self.stop(failed=True)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/VoIP/phone.py", line 306, in stop
    self.sip.stop()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 239, in stop
    self.deregister()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1210, in deregister
    deregistered = self.__deregister()
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 1232, in __deregister
    conn = self.send(first_request)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/SIP/client.py", line 250, in send
    return self.s.send(request.encode("utf8"))
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/sock/sock.py", line 473, in send
    conn.send(data)
  File "/home/ec2-user/.local/lib/python3.8/site-packages/pyVoIP/sock/sock.py", line 68, in send
    self.sock.s.sendto(data, addr)
OSError: [Errno 9] Bad file descriptor

Can you check this out?
Thank you, again.

@tayler6000
Copy link
Owner

Yes, this is exactly what I needed, and this should be an easy error to fix. Thank you.

tayler6000 added a commit that referenced this issue Jan 17, 2024
Fixed #217

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
[FIX] Fixed error being raised with qop not present
@tayler6000 tayler6000 added this to the pyVoIP 2.0 milestone Jan 17, 2024
@tayler6000
Copy link
Owner

This is now be fixed in 2.0.0a4

@github-project-automation github-project-automation bot moved this from Todo - Release to Done in pyVoIP Jan 17, 2024
This was referenced Jan 17, 2024
@shakir-snakescript
Copy link

Hi @tayler6000 I hope you're doing good. I also need your help with same issue. My code was working with stable version which is 1.6.8 but I want to use call transfer feature so I upgraded to version 2.0.0a4. But I am not able to make it work.
Could you please help?

old code (1.6.8)

import logging
from pyVoIP.VoIP import VoIPPhone, InvalidStateError, NoPortsAvailableError, PhoneStatus
import socket
import time

# Setup logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

def get_local_ip():
    """Get the local IP address."""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # Use a dummy address to get the local IP address
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except Exception:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

# Define your SIP credentials and server
sip_server = "sip_server"
sip_port = sip_port
sip_username = "username"
sip_password = "my_password"
local_ip = get_local_ip()
rtp_port_low = 10000
rtp_port_high = 20000

# Define the callback for incoming calls
def answer(call):
    try:
        call.answer()
        logger.info("Call answered.")
        call.hangup()
        logger.info("Call ended.")
    except InvalidStateError:
        logger.error("Invalid operation based on the current state of the call.")
    except NoPortsAvailableError:
        logger.error("No RTP ports available.")
    except Exception as e:
        logger.error(f"An error occurred: {e}")

if __name__ == "__main__":
    logger.info("Starting VoIP client...")

    # Initialize VoIP client
    phone = VoIPPhone(
        sip_server,
        sip_port,
        sip_username,
        sip_password,
        callCallback=answer,
        myIP=local_ip,
        sipPort=5060,
        rtpPortLow=rtp_port_low,
        rtpPortHigh=rtp_port_high
    )

    try:
        # Start the client
        phone.start()
        logger.info("VoIP client started. Waiting for registration and calls...")

        # Wait for SIP registration
        while True:
            if phone._status == PhoneStatus.REGISTERED:
                logger.info("SIP registration successful.")
                break
            elif phone._status == PhoneStatus.FAILED:
                logger.error(f"Failed to register with the SIP server. Final status: {phone._status}")
                break
            else:
                logger.debug(f"Current registration status: {phone._status}")
                time.sleep(1)

        logger.info("Press Ctrl+C to stop the phone and exit.")
        while True:
            pass

    except KeyboardInterrupt:
        logger.info("Stopping VoIP client...")
    except Exception as e:
        logger.error(f"An error occurred: {str(e)}")
    finally:
        # Ensure the client is stopped properly
        phone.stop()
        logger.info("VoIP client stopped.")

new code (2.0.0a4)

import pyVoIP; pyVoIP.DEBUG = True

from pyVoIP.credentials import CredentialsManager
from pyVoIP.VoIP.call import VoIPCall
from pyVoIP.VoIP.error import InvalidStateError
from pyVoIP.VoIP.phone import VoIPPhone, VoIPPhoneParameter, PhoneStatus
from pyVoIP.networking.transport import TransportMode
import time
import socket
import logging
import dns.resolver

# Increase logging level for more detailed output
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

def get_local_ip():
    """Get the local IP address."""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # Use a dummy address to get the local IP address
        s.connect(('10.255.255.255', 1))
        IP = s.getsockname()[0]
    except Exception:
        IP = '127.0.0.1'
    finally:
        s.close()
    return IP

def resolve_dns(domain):
    """Resolve DNS and return IP address."""
    try:
        result = dns.resolver.resolve(domain, 'A')
        return result[0].to_text()
    except dns.exception.DNSException as e:
        logger.error(f"DNS resolution failed: {e}")
        return None

# Modify SIP credentials and server details
sip_server = "sip_server"
sip_port = sip_port
sip_username = "username"
sip_password = "password"
local_ip = get_local_ip()

# Resolve SIP server IP
sip_server_ip = resolve_dns(sip_server)
if not sip_server_ip:
    logger.error("Failed to resolve SIP server IP. Exiting.")
    exit(1)

class Call(VoIPCall):
    def ringing(self, invite_request):
        try:
            self.answer()
            print("Answered")
            self.hangup()
        except InvalidStateError:
            pass
        except Exception:
            self.hangup()

if __name__ == "__main__":
    cm = CredentialsManager()
    cm.add(sip_username, sip_password)
    params = VoIPPhoneParameter(
        sip_server_ip, 
        sip_port, 
        sip_username, 
        cm, 
        bind_ip=local_ip, 
        call_class=Call, 
        transport_mode=TransportMode.TCP,
    )
    phone = VoIPPhone(params)

    phone.start()
    
    # Check registration status
    while phone._status != PhoneStatus.REGISTERED:
        logger.debug(f"Current registration status: {phone._status}")
        time.sleep(1)
    
    logger.info("SIP registration successful.")
    
    input('Press enter to disable the phone')
    phone.stop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

3 participants