Python NameError: name 'name' is not defined [closed]

不羁的心 提交于 2019-12-27 06:19:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!