I am trying to get the output after the lolimessage was sent but i get no output
try:
xmr = ''
data = s.recv(1024)
d = data.decode("UTF-8")
xmr += d
if "loli" in xmr:
s.send(lolimessage + enter)
print("Loli Sent")
else:
print("loli NOT sent")
except:
s.close()
try:
xmr = ''
data = s.recv(1024)
d = data.decode("UTF-8")
xmr += d
print(xmr)
except:
print("fail to print")
s.close()
The loli part works and prints out data but on the 2nd try i get "fail to print", can any1 plz help
s.send(lolimessage + enter)
send it in bytes with encoding
s.send(bytes(lolimessage + enter, "utf-8"))
Related
I write a spider ,it handshake and extend handshake is ok,and i success download metadata but when bdecode ,except failnot a valid bencoded string
i receive data like :
b'\x00\x00</\x14\x01d8:msg_typei1e5:piecei0e10:total_sizei15360eed6:lengthi800927755e4:name12:Zet9 Geo.rar12:piece lengthi1048576e6:pieces15280:3}\xdf\xd64\x90\x15z\x96aN\xe1\x08\xffU\x8c\xbd0m\xc0\xbb\x16\xd5\x13\xad\x9e\xb4\xa1\x1c\x02s\xe2\xc2\x1f\x11\xab\x1c-\xe0\x89{\xd8G\xfbFN\x0e~\xce\xb6pWG!%\xc4\xa3O<{^y0\'\xad\x92\xd2\xf3\xb7Hadq\xc3\x13"\xed\xdb\xfdx}\x05\x0e\xcd\x1e\xfc\xf4\xdc5\x80N\xe8\x8b\xbc\\\x10\x82\xf3i\x08\x81U\xa6\xb0\xf67\xb8\xaa\xebx\xe2\xe9jNv\x87\xfb\x0c\xb9$8\xc5s~\x0f{\x8f\xa4\xa8\x95\x08\xd9\r,X\nz\x9d\x87\x1e/\x1a\x00\x8d\xb3\xbc\x14\x08\xc7\x80\x0b\xecO\xb4M]\x81\xfa\xe1\xb02 C\x8e\xda\xe2)K\xcf\xdf\xee}>\x16\x95\x11|8\x87\x94J<\xe9%\t\xfc!\x12\xe8>\nWR\xb3\x8fp\xeaZ\xb9\xcb\xc9\xe0J\xe6\x91\xde\x08\xd8\x86\x90~\xe3{j\xd0\xbd\x11\xf8\x8f\x9c}\xdc\x13\xd8\xeb\x0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\xa4\x99#\xac\x99\xc4\xc7\xae\x8b\x93\xa2\xa1vcQ\xb6\xab\r\x94\xb99\xa2\xbf\x9fr\x8c\x17p\xacH\xe5\x92p\xe9q\x92\x8e\x13d\\\x05\xe1TH\x86~\xe8LD\xa1\xe3\xb7\xed\xb4\x8d\xfb\xf6\xd3\xfcN\xa9\x14b\xb3g\x80\x07\x7f\xe3\xd2`\x1ds\x95\xc4}\xf0W\xc7\x96\x98\x97\xa1b\x9a\x89\xaarX\xecKP\xc0\xady \xfau\x88\xca\xb8T\xf5\xf8\x8e\x1f\x08\x7f\x8d\xab\xb6_\xf6\xe7\x17\t\x1bQ\x1b\r)Z#\x19+HTv\xd9\xa2\r\x13\xb4)\xf6\x8e\x8fB\x14F2\x1e\xc8m\xc9N{l\xd5\'\xc13\xc2e\xa7U\\H(-\xab\xd8\xff6l\xbd\xb2\xf1\xf3\xd8)q\xfd\xb5\x1c\xb0\xed>B\xb8+$Tj\xe3OD\xa9\x0f\x0e\xda\xda\x8cZe.\xb5\x9e%\xbc\x9c\x0b\xf2\xe7=gz\xf1D\x05r\x88\xec\x87fd\x1fg\t (\xdf\x89\xc4\x82\xbb~#\xc3\xe6\xb0[\xb9\x82\x82n\x08\x10\xa3\x84\x00`\x00\x8dO\x93A\xb8%;\xde\x10'
i append a 'e' keep still fail,so i think problem in pieces
>>> bencoder.bdecode(b'd6:lengthi800927755e4:name12:Zet9 Geo.rar12:piece lengthi1048576e6:pieces4:1234e')
I get this:
{b'length': 800927755, b'name': b'Zet9 Geo.rar', b'piece length': 1048576, b'pieces': b'1234'}
def request_metadata(_socket,extHandShakeId,piece,timeout=20):
msg = chr(20) + chr(extHandShakeId) + bencoder.bencode({"msg_type":0,"piece":piece}).decode()
msgLen = pack(">I",len(msg))
msg = msgLen+msg.encode()
_socket.send(msg)
#_socket.setblocking(0)
total = b''
start = time()
data = _socket.recv(20*1024)
return data
a piece size 16*1024byte,_socket.recv(20*1024) can't receive all of piece?
The problem over here:
data = _socket.recv(20*1024)
function replace this:
def request_metadata(_socket,extHandShakeId,piece,timeout=20):
msg = chr(20) + chr(extHandShakeId) + bencoder.bencode({"msg_type":0,"piece":piece}).decode()
msgLen = pack(">I",len(msg))
msg = msgLen+msg.encode()
_socket.send(msg)
total = b''
while True:
try:
data = _socket.recv(20*1024)
if data:
total = total + data
except socket.timeout as e:
print(str(e))
break
return total
end i can bdecode that metadata
{b'length': 800927755, b'name': b'Zet9 Geo.rar', b'piece length': 1048576, b'pieces': b'3}\xdf\xd64\x90\x15z\x96aN........
I am trying to send textual commands to a microcontroller through usb serial port (ttyUSB0), the controller should respond with 'Y' or 'N' and execute the command. Commands are given in the following form '#01a' where the # is beginning symbol 0 is position for A channel and 1 position for B channel and 'a' is checksum of A+B.
I'm stuck and a beginner in python so any help is welcome and appreciated.
p.s. when I connect using putty everything works as expected
also the OS is Ubuntu 16.04 LTS
This is my code:
import time
import serial
import binascii
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=19200,
)
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
AT = chr(int('1000000',2))
A = chr(int('100000',2))
B = chr(int('100000',2))
AB = chr(int('1000000',2))
input = AT + A + B + AB
print input
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
#write data
ser.write(input)
# print("write data: AT+CSQ")
time.sleep(0.5) #give the serial port sometime to receive the data
numOfLines = 0
while True:
response = ser.readline()
print("read data: " + response)
numOfLines = numOfLines + 1
if (numOfLines >= 5):
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
Hello I have trouble with read/write from or to device. I wanted to make a serial connection via rs 485 (half-duplex) . When I call read and write functions they didnt receive a data. Anyone know what I do wrong?
def Transmission(ser,data):
if ser.isOpen():
try:
print(data)
ser.flushInput()
ser.flushOutput()
ser.rtscts = True
ser.write(data)
time.sleep(0.1)
numOfLines = 0
print("write: " + data)
while True:
response = ser.readline()
print("read data: " + response)
ser.rtscts = False
numOfLines = numOfLines + 1
if(numOfLines >= 5):
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
return response
Terminal didnt show receiving data:
powah
write: powah
read data:
read data:
read data:
read data:
read data:
I tried to write with read in the loop (with changing ser.rtscts). How to fix that problem? Thank You
Reading data from the serial port:
readline() in the below code return the null vector, the reading data from the serial port is hexadecimal number like AABB00EF the putty gives me the output means the communication is working but nothing works via python
here is the code:
#!/usr/bin/python
import serial, time
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 115200
#ser.bytesize = serial.EIGHTBITS
#ser.parity = serial.PARITY_NONE
#ser.stopbits = serial.STOPBITS_ONE
#ser.timeout = None
ser.timeout = 1
#ser.xonxoff = False
#ser.rtscts = False
#ser.dsrdtr = False
#ser.writeTimeout = 2
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
#ser.flushInput()
#ser.flushOutput()
#time.sleep(0.5)
# numOfLines = 0
# f=open('signature.txt','w+')
while True:
response = ser.readline()
print len(response)
#f=ser.write(response)
print response
# numOfLines = numOfLines + 1
f.close()
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
readline will try to read until the end of the line is reached, if there is no \r or \n then it will wait forever (if you have a timeout it might work...) instead try something like this
ser.setTimeout(1)
result = ser.read(1000) # read 1000 characters or until our timeout occures, whichever comes first
print repr(result)
just use this code
ser = serial.Serial("/dev/ttyUSB0",115200,timeout=1)
print "OK OPENED SERIAL:",ser
time.sleep(1)# if this is arduino ... wait longer time.sleep(5)
ser.write("\r") # send newline
time.sleep(0.1)
print "READ:",repr(ser.read(8))
you can create a readuntil method
def read_until(ser,terminator="\n"):
resp = ""
while not resp.endswith(terminator):
tmp = ser.read(1)
if not tmp: return resp # timeout occured
resp += tmp
return resp
then just use it like
read_until(ser,"\r")
I am trying to code a server with the following code. It is threaded and all I need help with is this error.
Unhandled exception in thread started by <function threaded_client at 0x0000000003302158>
line 28, in threaded_client
data = conn.recv(2048)
OSError: [WinError 10038] An operation was attempted on something that is not a socket
This error I cannot solve and have tried to solve. I would really like to know how to fix it.
import socket
import sys
from _thread import *
import time
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host,port))
except socket.error as e:
print(str(e))
s.listen(5)
print("Waiting for a Connection...")
def threaded_client(conn):
conn.send(str.encode("Welcome, please write to the server.\r\n"))
userinput = ""
while True:
data = conn.recv(2048)
data = data.decode('utf-8')
if not data:
break
#data = data.decode('utf-8')
#Gather up the Input
if data == '\r\n':
userinput += data
#Do something with it
else:
reply = 'Server output: ' + userinput
conn.sendall(str.encode(reply))
userinput = ""
conn.close()
while True:
conn, addr = s.accept()
print("connected to: " +addr[0] + ': ' + str(addr[1]))
start_new_thread(threaded_client, (conn,))
Now I am having an issue with how the server interacts with the client. Image of my CMD window open bellow. Please provide code to fix.
For windows 8, please.
Check this part.
else:
reply = 'Server output: ' + userinput
conn.sendall(str.encode(reply))
userinput = ""
conn.close() #<--- here you are closing the connection
This should be outside the loop. Something like this.
while True:
data = conn.recv(2048)
data = data.decode('utf-8')
if not data:
break
#data = data.decode('utf-8')
#Gather up the Input
if data == '\r\n':
userinput += data
#Do something with it
else:
reply = 'Server output: ' + userinput
conn.sendall(str.encode(reply))
userinput = ""
conn.close() #<--- Notice the indent