Home > other >  Python socket connection issue, bosses come in, please
Python socket connection issue, bosses come in, please

Time:12-01

 
#! The/usr/bin/python
# - * - coding: utf-8 - * -- --
From the socket import *
The import time
The import timeit
The import re
The import OS
The import sys
The import json
The import subprocess
Import the collections
The import threading
The import ConfigParser


The HOST='192.168.0.177'
PORT=9998
APIKEY='123456'
BUFSIZ=1024
The INTERVAL=1
ADDR=(HOST, PORT)
PORBEPORT=80

TcpSerSock=socket (AF_INET SOCK_STREAM)
TcpSerSock. Bind (ADDR)
TcpSerSock. Listen (5)
While True:
Try:
Print (' waiting for the connection... ')
TcpCliSock, addr=tcpSerSock. The accept ()
Print ('... Connnecting from: 'and addr)
Data=https://bbs.csdn.net/topics/tcpCliSock.recv (BUFSIZ). Decode ()
If the data!=APIKEY:
TcpCliSock. Close ()
While True:
Array={}
Array [] 'MSG'='success'
Print (' send ')
TcpCliSock. Send ((json. Dumps (array) + "\ n"). The encode (" utf-8 "))

Except the Exception as e:
Print (' error close... ')
TcpCliSock. Close ()
TcpCliSock. Close ()

Above for py script code on the server to perform,
When I connect to the server computer program, sending 123456, he would have been circulation return my MSG: success,
But when my computer crashed or connection program crashes,
The server side still display ('... Connnecting from: '(' my computer IP, 38825)), and has been print (' send'), and send the MSG, server network process there show that my computer is still connected to port 9998 (here is very strange, my computer crashed or program crashes, why still in the connection, probably script there have been active connection I),
When I program to reconnect again, but can't receive MSG

To solve the solution is:
How to send MSG in circulation, whether the end connection still alive, my connection exception introduced, for example, py script will automatically closes the connection, this does not affect my next connection
  • Related