This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Sworddragon
Recipients Sworddragon
Date 2014-06-21.05:04:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1403327091.28.0.269013779192.issue21819@psf.upfronthosting.co.za>
In-reply-to
Content
If I'm receiving data from a socket (several bytes) and making the first call to socket.recv(1) all is fine but the second call won't get any further data. But doing this again with socket.recv(2) instead will successfully get the 2 bytes. Here is a testcase:


Script:

def icmp_packet(type, code, data):
	length_data = len(data)
	if length_data % 2 == 1:
		data += b'\x00'
	checksum = code | type << 8
	i = 0
	while i < length_data:
		checksum += data[i + 1] | data[i] << 8
		checksum = (checksum & 65535) + (checksum >> 16)
		i += 2
	return bytes([type]) + bytes([code]) + (checksum ^ 65535).to_bytes(2, 'big') + data

import socket

connection = socket.socket(proto = socket.IPPROTO_ICMP, type = socket.SOCK_RAW)
connection.settimeout(1)
connection.sendto(icmp_packet(8, 0, b'\x00\x00\x00\x00'), ('8.8.8.8', 0))
print(connection.recv(2))
connection.close()
connection = socket.socket(proto = socket.IPPROTO_ICMP, type = socket.SOCK_RAW)
connection.settimeout(1)
connection.sendto(icmp_packet(8, 0, b'\x00\x00\x00\x00'), ('8.8.8.8', 0))
print(connection.recv(1))
print(connection.recv(1))
connection.close()


Here is the result:

root@ubuntu:/home/sworddragon/tmp# python3 test.py
b'E\x00'
b'E'
Traceback (most recent call last):
  File "test.py", line 24, in <module>
    print(connection.recv(1))
socket.timeout: timed out
History
Date User Action Args
2014-06-21 05:04:51Sworddragonsetrecipients: + Sworddragon
2014-06-21 05:04:51Sworddragonsetmessageid: <1403327091.28.0.269013779192.issue21819@psf.upfronthosting.co.za>
2014-06-21 05:04:51Sworddragonlinkissue21819 messages
2014-06-21 05:04:50Sworddragoncreate