问题
I am doing socket programming in python and finding a message. This is my code.....
import socket
def Main():
host = '127.0.0.1'
port = 5000
s = socket.socket()
s.bind((host,port))
s.listen(2)
c, addr = s.accept()
print "Connecton from : " + str(addr)
while True:
data = c.recv(1024)
if not data:
break
print "from connected User:" + str(data)
data = str(data).upper()
print "sending: " + str(data)
c.send()
c.close
if _name_ == '_main_':
Main()
and i recieve a message like
'File "tcpserver.py", line 21, in if name == 'main': NameError: name 'name' is not defined'
回答1:
It should be:
if __name__ == '__main__':
回答2:
Well, it should be:
if __name__ == '__main__':
Main()
With two (2) underscores, for both.
来源:https://stackoverflow.com/questions/37718214/python-nameerror-name-name-is-not-defined