Edit: I extended the question to HTTPS. The "S" part is not yet solved and I don't really need it, but it might be interesting for others.
I want to do the equivalent of
hdr='GET / HTTP/1.1\r\nContent-Length: 10000000000\r\n\r\n'
(echo -en "$hdr"; dd if=/dev/zero bs=1000000 count=999; read tmp) | nc $SOME_IP 80
with Python 2.7. If possible, I'd like to use only standard library plus the requests and sockets modules.
FYI, the above script sends a large HTTP request (~1GB of zeros) to $SOME_IP without being to heavy on the senders RAM.
Something like this?
import socket
def get_content_length():
return 10000000000
def get_chunk_size():
return 1000000
def yield_content():
total_size = get_content_length()
chunk_size = get_chunk_size()
current_size = 0
end = total_size - chunk_size
chunk = '\x00' * chunk_size
while current_size < end:
yield chunk
current_size += chunk_size
yield chunk[:total_size - current_size]
s = socket.socket()
s.connect((host, port))
hdr='GET / HTTP/1.1\r\nContent-Length: %s\r\n\r\n' % get_content_length()
s.sendall(hdr)
for chunk in yield_content():
s.sendall(chunk)
# should I wait for the response?
Here is what I cooked up and seems to work:
import sys, socket
def flood(hostname, port, count=1):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((hostname, port))
s.sendall('GET / HTTP/1.1\r\nContent-Length: 10000000000\r\n\r\n')
with open('/dev/zero', 'rb', ) as data:
for _ in xrange(count):
s.sendall(data.read(1000000))
s.shutdown(socket.SHUT_WR)
while True:
data = s.recv(1024)
if data == "":
break
print("Received:", repr(data))
print("Connection closed.")
s.close()
The answer by freakish is of course more cross-plattform, as it does not require /dev/null
Related
I am using python 2 in asterisk 2 there is a section where code listen to callers audio and process the audio. During the process there is a silence of 15 sec before the audio is played. I want to add a music during the processing of audio. Is there a way to do this.
the extension.config is like this
[autoattendant2]
exten => 5555,1,same=>n,Answer()
same=> n, AGI(/root/code_base/Queue/code2.py)
same=>n,hangup()
Below is the python code
#!/usr/bin/env python2
import sys
import re
import time
import random
import subprocess
import requests
import json
from datetime import datetime
env = {}
tests = 0;
while 1:
line = sys.stdin.readline().strip()
if line == '':
break
key,data = line.split(':')
if key[:4] <> 'agi_':
#skip input that doesn't begin with agi_
sys.stderr.write("Did not work!\n");
sys.stderr.flush()
continue
key = key.strip()
data = data.strip()
if key <> '':
env[key] = data
sys.stderr.write("AGI Environment Dump:\n");
sys.stderr.flush()
for key in env.keys():
sys.stderr.write(" -- %s = %s\n" % (key, env[key]))
sys.stderr.flush()
def checkresult (params):
params = params.rstrip()
if re.search('^200',params):
result = re.search('result=(\d+)',params)
if (not result):
sys.stderr.write("FAIL ('%s')\n" % params)
sys.stderr.flush()
return -1
else:
result = result.group(1)
#debug("Result:%s Params:%s" % (result, params))
sys.stderr.write("PASS (%s)\n" % result)
sys.stderr.flush()
return result
else:
sys.stderr.write("FAIL (unexpected result '%s')\n" % params)
sys.stderr.flush()
return -2
def change_file(path, cid):
# one of the process example
filename = 'complain{0}'.format(cid)
#filename =
input_file = path + '/' + filename + '.gsm'
output_file = path + '/' + filename + '.wav'
#command = "sox {} -r 8000 -c 1 {}".format(input_file, output_file)
command = "sox {} -b 16 -r 44100 -c 1 {} trim 0 7 vol 2".format(input_file, output_file)
subprocess.call(command, shell=True)
pbcheck = requests.get("http://127.0.0.1:8000/STT_complaint/", params = {"address" : output_file, "lang" : language, "cid":cid, "phone":callerid, "start_time":now})
res = pbcheck.content
res2 = res.replace('"', "")
return res2
def run_cmd(cmd):
#This runs the general command
sys.stderr.write(cmd)
sys.stderr.flush()
sys.stdout.write(cmd)
sys.stdout.flush()
result = sys.stdin.readline().strip()
checkresult(result)
#language = "ben"
# asking problem recorded audio
cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/{0}/plz_tell_problem \"\"\n".format(language, language)
run_cmd(cmd_streaming)
# listening to caller / recording caller voice
cmd_record = "RECORD FILE {0}/complain{1} gsm 1234 {2} s=3 \"\"\n".format(fixed_path, cid, 15)
run_cmd(cmd_record)
#processing audio
processed_output = change_file(path , cid) # while this is executing ad giving output i want to play wait_music and then stop to run
# while processing play this
cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/wait_music \"\"\n".format(language, language)
run_cmd(cmd_streaming)
# once output received (processed_output) play next audio
cmd_streaming = "STREAM FILE /root/code_base/recorded_voices/next_instruction \"\"\n")
run_cmd(cmd_streaming)
For that asterisk AGI have the special command "SET MUSIC ON"
https://wiki.asterisk.org/wiki/display/AST/Asterisk+18+AGICommand_set+music
Set waiting tone for asterisk agi function processing
I am trying to create a website for myself. It is kind of a Youtube on a local network. I am using video.js (have also tried Plyr.io) to play the video, but i can not fast forward the video. Or go back in the video. I can only play it from begining to the end. If i try to skip forward it only resets. What am I doing wrong?
Thanks in advance!
The behaviour sounds like the server doesn't implement range headers. When you try to seek, it returns the start of the file and not the part requested. If you try Safari you'll probably find it won't play at all. Check questions like Byte Ranges in Django
Yes, I all face the similar issue in using the video js library. But with the help from Byte range in django I solve this issue in video.js library by adding the RangeMiddleware. But I can skip or forward the video.
class RangesMiddleware(MiddlewareMixin):
def process_response(self, request, response):
if response.status_code != 200 or not hasattr(response, "file_to_stream"):
return response
http_range = request.META.get("HTTP_RANGE")
if not (
http_range
and http_range.startswith("bytes=")
and http_range.count("-") == 1
):
return response
if_range = request.META.get("HTTP_IF_RANGE")
if (
if_range
and if_range != response.get("Last-Modified")
and if_range != response.get("ETag")
):
return response
f = response.file_to_stream
statobj = os.fstat(f.fileno())
start, end = http_range.split("=")[1].split("-")
if not start: # requesting the last N bytes
start = max(0, statobj.st_size - int(end))
end = ""
start, end = int(start or 0), int(end or statobj.st_size - 1)
assert 0 <= start < statobj.st_size, (start, statobj.st_size)
end = min(end, statobj.st_size - 1)
f.seek(start)
old_read = f.read
f.read = lambda n: old_read(min(n, end + 1 - f.tell()))
response.status_code = 206
response["Content-Length"] = end + 1 - start
response["Content-Range"] = "bytes %d-%d/%d" % (start, end, statobj.st_size)
return response
I have python code that acquires serial data from 2 devices and writes to a .txt file. Every 4-15 minutes there is approx 30-45 seconds of data missing in the .txt file and this is not acceptable for our use case. I've spent hours googling and searching SO about multiprocessing and serial port data acquisition and haven't come up with a solution.
Here is my code
gpsser = input(("Enter GPS comport as 'COM_': "))
ser = serial.Serial(port=gpsser,
baudrate=38400,
timeout=2,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS)
root = Tk()
root.title("DualEM DAQ")
path = filedialog.asksaveasfilename() + ".txt"
file = glob.glob(path)
filename = path
with open(filename, 'wb') as f:
w = csv.writer(f, dialect='excel')
w.writerow(['header'])
def sensor():
while True:
try:
NMEA1 = dser.readline().decode("ascii")
while dser.inWaiting() == 0:
pass
NMEA1_array = NMEA1.split(',')
NMEA2_array = NMEA2.split(',')
NMEA3_array = NMEA3.split(',')
NMEA4_array = NMEA4.split(',')
if NMEA1_array[0] == '$PDLGH':
value1 = NMEA1_array[2]
value2 = NMEA1_array[4]
if NMEA1_array[0] == '$PDLG1':
value3 = NMEA1_array[2]
value4 = NMEA1_array[4]
if NMEA1_array[0] == '$PDLG2':
value5 = NMEA1_array[2]
value6 = NMEA1_array[4]
return (float(value1), float(value2), float(value3),
float(value4), float(value5), float(value6),
except (IndexError, NameError, ValueError, UnicodeDecodeError):
pass
def gps():
while True:
try:
global Status, Latitude, Longitude, Speed, Truecourse, Date
global GPSQuality, Satellites, HDOP, Elevation, Time
while ser.inWaiting() == 0:
pass
msg = ser.readline()
pNMEA = pynmea2.parse(msg)
if isinstance(pNMEA, pynmea2.types.talker.RMC):
Latitude = pynmea2.dm_to_sd(pNMEA.lat)
Longitude = -(pynmea2.dm_to_sd(pNMEA.lon))
Date = pNMEA.datestamp
Time = datetime.datetime.now().time()
if () is not None:
return (Longitude, Latitude, Date, Time)
except (ValueError, UnboundLocalError, NameError):
pass
while True:
try:
with open(filename, "ab") as f:
data = [(gps() + sensor())]
writer = csv.writer(f, delimiter=",", dialect='excel')
writer.writerows(data)
f.flush()
print(data)
except (AttributeError, TypeError) as e:
pass
The program is writing to the file but I need help understanding why I'm losing 30-45 seconds of data every so often. Where is my bottle neck that is causing this to happen?
Here is an example of where the breaks are, note the breaks are approx 50 seconds in this case.
Breaks in writing data to csv
DB
Back when I used PySerial, I did this:
nbytes = ser.inWaiting()
if nbytes > 0:
indata = ser.read(nbytes)
#now parse bytes in indata to look for delimiter, \n in your case
#and if found process the input line(s) until delimiter not found
else:
#no input yet, do other processing or allow other things to run
#by using time.sleep()
Also note that new versions (3.0+) of PySerial have .in_waiting as a property not a method, so no (), it used to be .inWaiting().
You should not flush the serial port input. Data is arriving on its own timing into a buffer in the driver, not when your read happens, so you are throwing away data with your flush. You may need to add code to synchronize with the input stream.
I used threading with a queue and changed my mainloop to look like this.
while True:
try:
with open(filename, "ab") as f:
writer = csv.writer(f, delimiter=",", dialect='excel')
data = []
data.extend(gpsdata())
data.extend(dualemdata())
writer.writerows([data])
f.flush()
f.close()
dser.flushInput()
ser.flushInput()
print(data)
sleep(0.05)
except (AttributeError, TypeError) as e:
pass
I had to flush the serial port input data before the looping back to the read functions so it was reading new realtime data(this eliminated any lag of the incoming data stream). I've ran a 30 minute test and the time gaps appear to have go away. Thankyou to Cmaster for giving me some diagnostic ideas.
I am trying to read-out a serial port.
The problem is that the script works but the read command seems to not respect the argument (2 bytes to read).
The typical output of the script (the ans variable in return function "readPosValue") is:
print(currTime,readPosValue(serPort))
(1517909247.176, '0b11000010110111001110011')
They are clearly more than 16 bits.
The used script:
import time
import struct
import binascii
import serial
ser = serial.Serial(
port='COM2',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
def inficonAcquisition(serPort):
try:
while True:
position = readPosValue(serPort)
currTime = time.time()
print(currTime,position)
except KeyboardInterrupt:
serPort.close()
exit()
def readPosValue(serPort):
ans = ''
while serPort.inWaiting() > 0:
ans += serPort.read(2)
return bin(int(binascii.hexlify('ans'), 16))
The problem was in the inWaiting() function.
It was not required in this kind of readout.
I have my code that I have simplified, it is as follows:
def my_function():
threading.Timer(10.0, my_function).start() # 10 second the function re-runs
# A TCP socket function
my_function()
While this works, and restarts the function every 10 seconds, the socket remains open, therefore I receive an error, that the data cannot be extracted due to the socket being open etc.
Is there a way to ensure that the socket closes at the end of the function? Thus re-opening with each restart of the function.
The full function code is below.
def iothub_client_sample_run():
threading.Timer(10.0, iothub_client_sample_run).start() # 10 is seconds - change stop to start
try:
client = iothub_client_init()
if client.protocol == IoTHubTransportProvider.MQTT:
print ( "IoTHubClient is reporting state" )
reported_state = "{\"newState\":\"standBy\"}"
client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
telemetry.send_telemetry_data(parse_iot_hub_name(), EVENT_SUCCESS, "IoT hub connection is established")
while True:
global MESSAGE_COUNT,MESSAGE_SWITCH
if MESSAGE_SWITCH:
# send a few messages every minute
print ( "IoTHubClient sending %d messages" % MESSAGE_COUNT )
while True:
try:
ip = 'xxxx'
port = 'xxxx'
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((ip,port))
print 'connecting...'
while True:
try:
ready_to_read, ready_to_write, in_error = \
select.select([conn,], [conn,],[], 5)
except:
pass
print 'connection error'
break
if len(ready_to_read) > 0:
recv2 = conn.recv(2048)
try:
recv3 = recv2.split(',')
date = float(recv3[2])
time = float(recv3[3])
heave= float(recv3[4])
north= float(recv3[5])
east = float(recv3[6])
msg_txt_formatted = MSG_TXT % (date,time,heave,north,east)
try:
print (msg_txt_formatted)
message = IoTHubMessage(msg_txt_formatted)
# optional: assign ids
message.message_id = "message_%d" % MESSAGE_COUNT
message.correlation_id = "correlation_%d" % MESSAGE_COUNT
# optional: assign properties
prop_map = message.properties()
#prop_map.add("temperatureAlert", "true" if temperature > TEMPERATURE_ALERT else "false")
client.send_event_async(message, send_confirmation_callback, MESSAGE_COUNT)
print ( "IoTHubClient.send_event_async accepted message [%d] for transmission to IoT Hub." % MESSAGE_COUNT )
status = client.get_send_status()
print ( "Send status: %s" % status )
MESSAGE_COUNT += 1
time.sleep(config.MESSAGE_TIMESPAN / 4000.0)
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
telemetry.send_telemetry_data(parse_iot_hub_name(), EVENT_FAILED, "Unexpected error %s from IoTHub" % iothub_error)
return
except:
print sys.exc_info()[0]
except:
print 're-connecting'
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
iothub_client_sample_run()