Send data with sockets - sockets

I have 2 ESP8266 connected in wifi.
The client sends its local time
def heure():
annee = str(time.localtime()[0])
mois = str(time.localtime()[1])
jour = str(time.localtime()[2])
hours = str(time.localtime()[3])
mins = str(time.localtime()[4])
if int(mins) < 10:
mins = '0' + mins
timestr = annee + ':' + mois + ':' + jour +':' +hours + ':' + mins
return(timestr)
def emission(donnees):
adresse_serveur=socket.getaddrinfo("192.168.144.2",800)[0][-1]
com_net=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
com_net.connect(adresse_serveur)
com_net.send(donnees)
com_net.close()
timestr=heure()
donnees=str(timestr)
emission(donnees)
print (donnees) ---> 2023:2:1:18:08
On the server:
new_list=[]
conn, addr = s.accept()
reception_donnees=conn.recv(512)
for item in reception_donnees.split(b':'):
new_list.append(item.decode('ascii'))
conn.close()
print (new_list) ---> ['2000', '1', '1', '0', '00'] ???
I see that the client sends 2023 and the server shows 2000
Where is the error?
I searched the web but couldn't find anything

Related

QUICBASIC 4.5 Program now in QB64

I took a QB45 application and produced a QB64 application from it over 100,000 lines of code. All the pricing was hard coded in the program so I started reading from CSV files but now I need the date from the CSV file without putting a directory to a text file and reading for the date. I found this code below
The problem when I run it is that the assembler is for 16 bit registers and I am using 32 or 64 with windows 7 and i5 core. Can anyone help me to figure out how the date will be returned in a longer int value from the register?
'===========================================================================
' Subject: GET/SET FILE DATE/TIME Date: Unknown Date (00:00)
' Author: Matt Hart Code: QB, PDS
' Keys: GET,SET,FILE,DATE,TIME Packet: DOS.ABC
'===========================================================================
' FILEDATE.BAS by Matt Hart
'
' Gets or sets a file date/time
'
' GetFileDateTime returns the Date in MM-DD-YYYY format
' and the Time in HH:MM:SS
' SetFileDateTime expects the Date and Time in the same formats
'$INCLUDE: 'QB.BI' ' Use your path to QB or QBX.BI
DEFINT A-Z
DECLARE SUB GetFileDateTime (F$, Dat$, Tim$, Ecode%)
DECLARE SUB SetFileDateTime (F$, Dat$, Tim$, Ecode%)
' ------------------------- Sample code
F$ = LTRIM$(RTRIM$(COMMAND$))
CALL GetFileDateTime(F$, Dat$, Tim$, Ecode)
IF NOT Ecode THEN
PRINT F$; " date is "; Dat$
PRINT F$; " time is "; Tim$
ELSE
PRINT "1 Error = "; Ecode
END
END IF
NewTim$ = "01:01:02"
NewDat$ = "02-02-1980"
CALL SetFileDateTime(F$, NewDat$, NewTim$, Ecode)
IF Ecode THEN
PRINT "2 Error = "; Ecode
END
END IF
CALL GetFileDateTime(F$, Dat$, Tim$, Ecode)
IF Ecode THEN
PRINT "3 Error = "; Ecode
END
END IF
PRINT F$; " new date is "; Dat$
PRINT F$; " new time is "; Tim$
CALL SetFileDateTime(F$, Dat$, Tim$, Ecode)
IF Ecode THEN
PRINT "4 Error = "; Ecode
END
END IF
END
' ------------------------------------
SUB GetFileDateTime (F$, Dat$, Tim$, Ecode)
Ecode = 0
DIM InRegs AS RegTypeX
DIM OutRegs AS RegTypeX
InRegs.ax = &H3D00 ' Open file function
DIM FileName AS STRING * 128 ' Use fixed length
FileName = F$ + CHR$(0) ' Must be ASCIIZ string
InRegs.ds = VARSEG(FileName) ' Fixed length makes these
InRegs.dx = VARPTR(FileName) ' come out right
CALL INTERRUPTX(&H21, InRegs, OutRegs) ' Open the file
IF NOT OutRegs.flags THEN ' No error
Handle = OutRegs.ax ' Save DOS file handle
InRegs.ax = &H5700 ' Get date/time function
InRegs.bx = Handle
CALL INTERRUPTX(&H21, InRegs, OutRegs)
HMS& = OutRegs.cx ' Use long integer for
IF HMS& < 0& THEN HMS& = 65536 + HMS& ' positive numbers
Hours = HMS& \ 2048& ' Hours is first 5 bits
Minutes = (HMS& AND 2047&) \ 31& ' Minutes is next 6 bits
Seconds = HMS& AND 31& ' Seconds is last 5 bits
H$ = LTRIM$(STR$(Hours))
M$ = LTRIM$(STR$(Minutes)): IF LEN(M$) = 1 THEN M$ = "0" + M$
S$ = LTRIM$(STR$(Seconds)): IF LEN(S$) = 1 THEN S$ = "0" + S$
Tim$ = H$ + ":" + M$ + ":" + S$
YMD& = OutRegs.dx ' Long int here too
IF YMD& < 0 THEN YMD& = 65536 + YMD& ' Convert to + if needed
Year = 1980& + YMD& \ 512& ' Year is first 7 bits
Month = (YMD& AND 511&) \ 31& ' Month is next 4 bits
Day = YMD& AND 31& ' Day is last 5 bits
Y$ = LTRIM$(STR$(Year))
M$ = LTRIM$(STR$(Month))
D$ = LTRIM$(STR$(Day)): IF LEN(D$) = 1 THEN D$ = "0" + D$
Dat$ = M$ + "-" + D$ + "-" + Y$
InRegs.ax = &H3E00 ' Close file function
InRegs.bx = Handle
CALL INTERRUPTX(&H21, InRegs, OutRegs) ' Close it
ELSE
Ecode = OutRegs.flags ' Otherwise return error flags
END IF
END SUB
SUB SetFileDateTime (F$, Dat$, Tim$, Ecode)
Ecode = 0
DIM InRegs AS RegTypeX
DIM OutRegs AS RegTypeX
InRegs.ax = &H3D00
DIM FileName AS STRING * 128
FileName = F$ + CHR$(0)
InRegs.ds = VARSEG(FileName)
InRegs.dx = VARPTR(FileName)
CALL INTERRUPTX(&H21, InRegs, OutRegs)
IF NOT OutRegs.flags THEN
Handle = OutRegs.ax
InRegs.ax = &H5701
InRegs.bx = Handle
Hours& = VAL(LEFT$(Tim$, 2)) * 2048&
Minutes& = VAL(MID$(Tim$, 4, 2)) * 32&
Seconds& = VAL(RIGHT$(Tim$, 2)) \ 2
HMS& = Hours& + Minutes& + Seconds&
IF HMS& > 65536 THEN
InRegs.cx = 65536 - HMS&
ELSE
InRegs.cx = HMS&
END IF
Year& = (VAL(RIGHT$(Dat$, 4)) - 1980&) * 512&
Month& = VAL(LEFT$(Dat$, 2)) * 32&
Day& = VAL(MID$(Dat$, 4, 2))
YMD& = Year& + Month& + Day&
IF YMD& > 65536 THEN
InRegs.dx = 65536 - YMD&
ELSE
InRegs.dx = YMD&
END IF
CALL INTERRUPTX(&H21, InRegs, OutRegs)
InRegs.ax = &H3E00
InRegs.bx = Handle
CALL INTERRUPTX(&H21, InRegs, OutRegs)
ELSE
Ecode = OutRegs.flags
END IF
END SUB

Unexpected EOF on client connection with an open transaction

I want to using threading to connect postgresql
but postgresql log show:
unexpected EOF on client connection with an open transaction
here is my code:
conn = []
for i in range(10):
conn.append(psycopg2.connect("dbname=test user=higis password=dbrgdbrg host=10.1.1.215 port=5432"))
print conn
def test_query(a,b,c,d,name,i):
try:
#conn = psycopg2.connect("dbname=test user=higis password=dbrgdbrg host=10.1.1.215 port=5432")
cur = conn[i].cursor()
sql_query = "SELECT count(*) FROM " + str(name) + " WHERE ST_MAKEENVELOPE" + str(
(a, b, c, d, 4326)) + "&& wkb_geometry"
start = time.time()
cur.execute(sql_query)
#conn.commit()
end = time.time()
results_time = end - start
results = cur.fetchall()
cur.close()
conn[i].close()
#print results
#print results[0][0]
suit_f = open("/home/pj/Desktop/test/log/replicate_query_time.txt", 'a')
print >> suit_f, (
'range query table:,%s,(%s %s %s %s),%s,%s' % (name, a, b, c, d, results[0][0], results_time))
except Exception, e:
print e
a_all = [1,2,3,4,5,6,7,8,9,10]
b_all = [1,2,3,4,5,6,7,8,9,10]
c_all = [1,2,3,4,5,6,7,8,9,10]
d_all = [1,2,3,4,5,6,7,8,9,10]
threads = []
for i in range(10):
a = a_all[i]
b = b_all[i]
c = c_all[i]
d = d_all[i]
t = threading.Thread(target=test_query,args=(a,b,c,d,"replicate_table1",i))
threads.append(t)
if __name__ == '__main__':
i = 0
for t in threads:
print "Thread:" + str(i) + " t and the time = %s" %(ctime())
t.setDaemon(True)
t.start()
i = i+1
t.join()
#conn.commit()
At some point the server received a client connection which presented the appropriate credentials (and thus a session was established).
This log message is informing you that the client disconnected the session "it just walked away!" without shutting down cleanly. Maybe the code is throwing an exception before it gets to the cur.close() statement.

Windows Server 2003 + VB6 + SMTP; html content-type not working

Server: Windows 2003 r3
Program is supposed to automatically send out emails when they appear in a folder.
The issue that I am having is I can't find any support for the object that is being created.
I am trying to send a email using the text/html content-type and the emails being sent keep being received with the text/plain and the html is just normally displaying as text
The object being created is:
oMail As SMTP
I've tried
oMail.IsBodyHtml = True
And
oMail.MessageFormat = 1
All the tutorials online i've seen use
Dim oSmtp As EASendMailObjLib.Mail
Below is the entire function
Public Function sEnd(oMail As SMTP, MailToSend() As OutMail, ByVal i As Integer) As Boolean
On Error GoTo SendEmail_Err
Dim result
' Reset Smtp err code
iSmtpErr = 0
' Go thru the list of Mail to send
With MailToSend(i)
If .Status = EMO_TO_SEND Then
DoEvents
''''''''''''''''''''''''''''''''''''''
' Load Winsock
oMail.WinsockLoaded = True
' Specify Domain, Host and Mail Port
oMail.MailServer = sOutboundDomain
oMail.MailPort = iOutboundMailPort
DoEvents
'oMail.Action = a_ConnectToServer
oMail.Action = a_ResetHeaders
' oMail.IsBodyHtml = True
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Specify Mail contents: Sender, Recipient, subject, body, ..
oMail.From = .SenderEmailAddr
oMail.To = .RecipientEmailAddr
oMail.SUBJECT = .SUBJECT
oMail.Date = Format(Now, "ddd, dd mmm yyyy hh:mm:ss") & Space(1) & sTimeZone
oMail.MessageText = .Body
' oMail.MessageFormat = 1
oMail.OtherHeaders = X_MAILER & Space(1) & sXMailer
' Send Message
oMail.Action = a_SendMessage
.Status = EMO_SENT
.TimeSent = Now
DoEvents
' Quit
oMail.Action = a_DisconnectFromServer
End If
End With
sEnd = True
Exit Function
SendEmail_Err:
On Error Resume Next
If iSmtpErr >= 25005 And iSmtpErr <= 26005 Or _
iSmtpErr = 20302 Or iSmtpErr = 20163 Or _
iSmtpErr = 20162 Then
' Changed to handle invalid email address error - July 20, 1999
' Or iSmtpErr = 0 Then
' Winsock/connection error
MailToSend(i).Status = EMO_NO_CONNECTION
RaiseAlert USER_CONNECTION_ERR, ""
Else
MailToSend(i).Status = EMO_FAILED_TO_SEND
End If
' Log error
Call LogError(CLng(iSmtpErr), MailToSend(i).FileName & ":" & Error, "Send")
' Put in to handle invalid email address error - July 20, 1999
oMail.Action = a_DisconnectFromServer
oMail.Action = a_Idle
sEnd = True
Exit Function
End Function
Thanks a lot for any input!

Lua Raw Socket Example

I'm having a hard time finding some example code for Raw Sockets in Lua. Ideally they should look something like they do in Python:
#create a raw socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
except socket.error , msg:
print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
# tell kernel not to put in headers, since we are providing it, when using IPPROTO_RAW this is not necessary
# s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
# now start constructing the packet
packet = '';
source_ip = '192.168.1.101'
dest_ip = '127.0.0.1' # or socket.gethostbyname('www.google.com')
# ip header fields
ip_ihl = 5
ip_ver = 4
ip_tos = 0
ip_tot_len = 0 # kernel will fill the correct total length
ip_id = 54321 #Id of this packet
ip_frag_off = 0
ip_ttl = 255
ip_proto = socket.IPPROTO_TCP
ip_check = 0 # kernel will fill the correct checksum
ip_saddr = socket.inet_aton ( source_ip ) #Spoof the source ip address if you want to
ip_daddr = socket.inet_aton ( dest_ip )
ip_ihl_ver = (ip_ver << 4) + ip_ihl
# the ! in the pack format string means network order
ip_header = pack('!BBHHHBBH4s4s' , ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)
# tcp header fields
tcp_source = 1234 # source port
tcp_dest = 80 # destination port
tcp_seq = 454
tcp_ack_seq = 0
tcp_doff = 5 #4 bit field, size of tcp header, 5 * 4 = 20 bytes
#tcp flags
tcp_fin = 0
tcp_syn = 1
tcp_rst = 0
tcp_psh = 0
tcp_ack = 0
tcp_urg = 0
tcp_window = socket.htons (5840) # maximum allowed window size
tcp_check = 0
tcp_urg_ptr = 0
tcp_offset_res = (tcp_doff << 4) + 0
tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5)
# the ! in the pack format string means network order
tcp_header = pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window, tcp_check, tcp_urg_ptr)
user_data = 'Hello, how are you'
If you guyshave some laying around or know where to find some. I haven't seen much from my google searching.
Thanks,
LuaSocket is not made for this. Maybe look at libpcap and libnet?

Convert function to Classic ASP

I am using a mailing list program which inserts a date into a web link that is "encoded" so it can't be changed or edited by users.
The format is described as follows:
An eight character string, AABBCCDD,where:
Year = 1980 + HexToInt(BB) / 3
Month = HexToInt(CC) / 7 - 21
Day = HexToInt(DD) / 7 - 5
There is also a checksum included to avoid casual modification:
AA = IntToHex(Year + Month + Day mod 200)
For example 2660BDAF would refer to 20 June, 2012.
Can you help me convert the following to Classic ASP:
CodedDateStr = Request.querystring("Exp")
AYear = 1980 + HexToInt(CodedDateStr[3] + CodedDateStr[4]) / 3
AMonth = HexToInt(CodedDateStr[5] + CodedDateStr[6]) / 7 - 21
ADay = HexToInt(CodedDateStr[7] + CodedDateStr[8]) / 7 - 5
ACheckSum = AYear + AMonth + ADay mod 200
if ACheckSum <> HexToInt(CodedDateStr[1] + CodedDateStr[2]) then
ValidDate = 0
else
ValidDate = 1
end if
AExpiryDate = EncodeDate(ADay, AMonth, AYear)
if Date() > AExpiryDate then
ExpiredOffer = 1
else
ExpiredOffer = 0
end if
....
It looks like the HexToInt equivalent is clng("&h" & hexnumber)
I'm not sure about EncodeDate, i hope it is not something cludgy like CDate(AMonth + "/" + ADay + "/" + AYear)
CLng("&h" & hexnumber) looks like a good method for HexToInt.
For EncodeDate, look at the DateSerial function, which takes a year, month, and day, and returns a Date value.